在catch块中抛出相同的异常

4

我有以下两个代码片段,想知道是什么让Java编译器(在Eclipse和Java 7中)对第二个代码片段显示错误,而对第一个不会。

以下是这两个代码片段:

代码片段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;
        }
    }
}

代码片段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中,片段1显示为finally块添加'SuppressWarning',但在片段2中,它显示为在catch块中的throw语句添加'throws或try-catch'块。我详细查看了以下问题,但它们没有提供任何具体原因。
3个回答

2
在代码段1中,您不需要使用return语句。在finally块中,Java已经知道在处理完finally块后要做什么:要么继续异常处理,要么跳转到finally块之后的下一条语句。
因此,在代码段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;
}

在代码片段2中,每个块都会抛出一个异常。由于这个异常不是未经检查的,所以必须在方法概要中指明它。此外,没有返回语句,方法的返回值也不是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;
    //      }
}

2
第二个片段没有返回值。它已经被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;
//      }

2
返回语句无法到达。其后的所有代码块都会引发异常。 - Stephan
是的,那很有道理。谢谢。 - ms_27

2
finally块应该始终执行,这是主要原因。异常在catch块中抛出,但finally块中执行的return语句掩盖了异常。要么删除finally块,要么将return语句移出finally块。

1
谢谢。所以,这都是关于掩码的问题。我们可以说,finally块始终会掩盖在catch块中抛出的异常。或者,也有一些标准来判断这个问题。 - ms_27
@nipkon 考虑到以下代码块,每次运行该块时,输出中的sysout和堆栈跟踪的顺序是不同的。 try{ System.out.println("在 try 中..."); int x = 10/0; }catch(Exception e){ System.out.println("在 catch 中..."); e.printStackTrace(); } finally{ System.out.println("在 finally 中..."); } - ms_27
抱歉无法在评论中使其看起来漂亮。 - ms_27

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接