try{
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new Exception("get remote source meta data timeout!");
}catch(Exception e){
//这什么也没做,异常被屏蔽了
}
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new Exception("get remote source meta data timeout!");
}catch(Exception e){
//这什么也没做,异常被屏蔽了
}
解决方案 »
- 请问java中所有的抽象类都要用 implements吗?
- JAVA的一个小问题
- 请问:我装java jdk时,后面怎么还有一个update,不知道是东西呀,不装行不行呀
- ResultSetMetaData类的问题请教
- 一个DataInputStream的问题
- 学JAVA10来天,写了个计算器!但是感觉布局不好,请指教!
- 为什么IE能浏览我下载的.class文件,而不能看我用jbuilder生成的.class文件
- 怎样在jbuilder 7。0中为applet程序创建菜单?
- 如何在jframe中导入两张图片(gif),让一个押在另一个上边……
- 关于字节流操作文本数据的问题
- 关于JOptionPane.showInputDialog的一个问题
- 哪里有java的帮助文档? 马上给分!
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new RuntimeException("get remote source meta data timeout!");
// }
//catch(Exception e){
// }
public boolean isTimeOut = false;
public MyTimerTask(){ }
public void run()throws Exception{
try{
isTimeOut = true;
logger.error("get remote source meta data timeout!");
throw new Exception("get remote source meta data timeout!");
}catch(Exception e){
throw e;
}
}错误信息:run() in MyTimerTask cannot override run() in java.util.TimerTask; overridden method does not throw java.lang.Exception at line 1,984 (1,984:5)怎么办呢? 我需要从这里抛出异常啊?或者有其他的解决方法吗?——做一个定时器,如果时间到了,抛出时间超时的异常!
int delay = 5 * 1000; //毫秒
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new Exception("Time out.");
testTimer.stop();
}
}
testTimer = new Timer(delay);
testTimer.start();
int delay = 5 * 1000; //毫秒
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
testTimer.stop(); //先停止
throw new Exception("Time out.");
}
}
testTimer = new Timer(delay);
testTimer.start();
2.在JAVA中Timer是一个线程,所以有了第一点。
3.一般的应用中,在Timer线程中都想访问Caller的对象,看看例程中我的做法
4.如果这样做,Caller的共享资源要synchronized保护起来
5.不要为了抛出异常而异常,Caller的Timeout能做到你想做的,但是它仍是Timer线程调用的,别指望在哪抛出异常能在main里catch到,main是另一个线程
6.通过这个例程,我自己也清晰了很多 :)package test;
import java.util.*;
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class test { public synchronized void Timeout(){
//log error
System.out.println("get remote source meta data timeout!");
}
public static void main(String[] args) {
try{
test t = new test();
System.out.println("Start..");
java.util.Timer timer = new java.util.Timer(true);
MyTimerTask mTask = new MyTimerTask(t);
timer.schedule(mTask,3000);
while(true);
}
catch(Exception e){
System.out.println("error:" + e.toString());
}
}
}
class MyTimerTask extends TimerTask {
test Caller = null;
public boolean isTimeOut = false;
public MyTimerTask(test t){
Caller = t;
}
public void run(){
isTimeOut = true;
Caller.Timeout();
}
}