【问题标题】:Throwing same exception inside catch block在 catch 块内抛出相同的异常
【发布时间】:2015-11-19 09:20:18
【问题描述】:

我有以下 2 个代码 sn-ps,我想知道是什么让 java 编译器(在带有 Java 7 的 Eclipse 中)显示第二个 sn-p 的错误,为什么不显示第一个。

这里是sn-ps的代码:

sn-p 1

public class TestTryCatch {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(get());
    }

    public static int get(){
        try{
            System.out.println("In try...");
            throw new Exception("SampleException");
        }catch(Exception e){
            System.out.println("In catch...");
            throw new Exception("NewException");
        }
        finally{
            System.out.println("In finally...");
            return 2;
        }
    }
}

sn-p 2

public class TestTryCatch {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(get());
    }

    public static int get(){
        try{
            System.out.println("In try...");
            throw new Exception("SampleException");
        }catch(Exception e){
            System.out.println("In catch...");
            throw new Exception("NewException");
        }
//      finally{
//          System.out.println("In finally...");
//          return 2;
//      }
    }
}

在 eclipse 中,sn-p1 显示为 finally 块添加“SuppressWarning”,但在 sn-p2 中,它显示为 catch 中的 throw 语句添加“throws or try-catch”块块。

我详细查看了以下问题,但没有提供任何具体原因。

【问题讨论】:

    标签: java exception exception-handling try-catch


    【解决方案1】:

    finally 块应该始终执行。这是主要原因。异常在catch 块中引发,但在finally 块中执行的return 语句屏蔽了异常。删除finally 块或将return 语句移出finally 块。

    【讨论】:

    • 谢谢。所以,这一切都与掩蔽有关。可以说,finally 块总是会掩盖在 catch 块中抛出的异常。或者,也有一些标准。
    • @nipkon 然后考虑下面的块,每次我们运行这个块时,sysout 的顺序和输出中的堆栈跟踪都是不同的。 try{ System.out.println("In try..."); int x = 10/0; }catch(Exception e){ System.out.println("In catch..."); e.printStackTrace(); } finally{ System.out.println("In finally..."); }
    • 抱歉,无法在 cmets 中让它看起来很漂亮。
    【解决方案2】:

    第二个 sn-p 没有返回值。已经用 finally 子句注释掉了。

    试试这个

    public static int get(){
        try{
            System.out.println("In try...");
            throw new Exception("SampleException");
        }catch(Exception e){
            System.out.println("In catch...");
            throw new Exception("NewException");
        }
        return 2;   // must return a value
    //      finally{
    //          System.out.println("In finally...");
    //          return 2;
    //      }
    

    【讨论】:

    • return 语句不可达。它后面的所有块都会引发异常。
    • 是的,这是有道理的。谢谢。
    【解决方案3】:

    在 sn-p 1 中,您不需要 return 语句。在 finally 块中,Java 已经知道处理 finally 块后要做什么:要么继续异常处理,要么跳转到 finally 块之后的下一条语句。

    所以在 sn-p 1 中,只需将 return 语句移出 finally 块。

    public static int get(){
        try {
            System.out.println("In try...");
            throw new Exception("SampleException");
        } catch(Exception e) {
            System.out.println("In catch...");
            throw new Exception("NewException");
        } finally{
            System.out.println("In finally...");
        }
    
        return 2;
    }
    

    在 sn-p 2 中,每个块中都会引发异常。由于此异常不是未经检查的,因此必须在方法概要中指出。另外,没有return语句,方法的返回值不是void

    只需在方法的末尾添加一个 throws 语句和一个 return 语句。

    public static void get() throws Exception {
        try{
            System.out.println("In try...");
            throw new Exception("SampleException");
        }catch(Exception e){
            System.out.println("In catch...");
            throw new Exception("NewException");
        }
        //      finally{
        //          System.out.println("In finally...");
        //          return 2;
        //      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多