如何从定时器timer中抛出异常,我想实现的很简单:做一个定时器,如果时间到了,抛出时间超时的异常!
try{
java.util.Timer timer = new java.util.Timer(true);
MyTimerTask mTask = new MyTimerTask();
timer.schedule(mTask,GET_SOURCE_METADATA_TIME);
……
……
}
catch(Exception e){
System.out.println("error:" + e.toString());
}
static class MyTimerTask extends TimerTask {
public boolean isTimeOut = false;
public MyTimerTask(){
}
public void run(){
try{
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new Exception("get remote source meta data timeout!");
}catch(Exception e){
}
}
}
上面的实现可以吗? 好像不能捕捉到异常啊!
------解决方法--------------------------------------------------------
try{
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new Exception("get remote source meta data timeout!");
}catch(Exception e){
//这什么也没做,异常被屏蔽了
}
------解决方法--------------------------------------------------------
//try{
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new RuntimeException("get remote source meta data timeout!");
// }
//catch(Exception e){
// }
------解决方法--------------------------------------------------------
用java.awt.Timer