【发布时间】:2015-06-25 05:02:51
【问题描述】:
我需要创建一个方法,如果我输入该方法并且如果我需要超过 30 秒来处理该方法,那么它应该抛出一个异常,我将立即退出该方法并且我可以处理它调用方法中的异常,以便我的下一个过程顺利进行。
public static void method() {
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@SuppressWarnings("finally")
@Override
public void run() {
try {
System.out.println("inner "+Thread.currentThread());
System.out.println("first ");
}
finally{ System.out.println("before return "); throw new RuntimeException("Sorry TimeOut");}
}
},400);
try{
System.out.println(Thread.currentThread());
Thread.sleep(1000);
}catch(InterruptedException e){}
System.out.println("OKKKKK");
//return null;
}
【问题讨论】:
标签: java exception exception-handling timertask throw