finally就像一个析构函数,因此我们可以重启这样的应用程序。警告:finally是最终确定的错误替代品,也不是正确的析构函数。
public class RestartDemo
{
public void oddeven(int x)
{
if(x % 2 != 0)
{
try
{
this.restart();
}
catch(Exception e)
{
//do nothing
}
}
else
{
System.out.println(x + " is even");
}
}
public void restart()throws Exception
{
try
{
throw new Exception("Force restart!");
}
finally
{
RestartDemo.main(new String[0]);
}
}
public static void main(String args[])
{
RestartDemo r = new RestartDemo();
r.oddeven((int)(Math.random() * 100));
}
}