try{
        isTimeOut = true;
        logger.error("get remote source meta data timeout!");
        throw new Exception("get remote source meta data timeout!");
      }catch(Exception e){
     //这什么也没做,异常被屏蔽了
      }

解决方案 »

  1.   

    //try{
            isTimeOut = true;
            logger.error("get remote source meta data timeout!");
            throw new RuntimeException("get remote source meta data timeout!");     
    // }
    //catch(Exception e){
    //      }
      

  2.   

    呀,对哦! 多谢提醒!不过又出问题啦!static class MyTimerTask extends TimerTask {
        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)怎么办呢? 我需要从这里抛出异常啊?或者有其他的解决方法吗?——做一个定时器,如果时间到了,抛出时间超时的异常!
      

  3.   

    用java.awt.TimerTimer testTimer = null; // 最后设成全局变量
    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();
      

  4.   

    用javax.swing.TimerTimer testTimer = null; // 最后设成全局变量
    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();
      

  5.   

    to  ralphvsclark(微笑) : 还是不行啊! 提示错误:unreported exception java.lang.Exception; must be caught or declared to be thrown at line 26 (26:9)
      

  6.   

    楼主,我写了一个例程,应该能满足你的要求注意几点:1.不可能跨线程catch异常,因为很明显线程使用不同的栈,而异常是基于栈的
    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();
    }
    }