Java 在catch块内引发相同的异常

Java 在catch块内引发相同的异常,java,exception,exception-handling,try-catch,Java,Exception,Exception Handling,Try Catch,我有以下两个代码段,我想知道是什么使java编译器(在带有java 7的Eclipse中)显示第二个代码段的错误,为什么不显示第一个代码段的错误 以下是代码片段: 片段1 public class TestTryCatch { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(get()); } p

我有以下两个代码段,我想知道是什么使java编译器(在带有java 7的Eclipse中)显示第二个代码段的错误,为什么不显示第一个代码段的错误

以下是代码片段:

片段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中,snippet1显示为finally块添加“SuppressWarning”,但在snippet2中,它显示为catch块中的throw语句添加“throws或try catch”块

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


在代码段1中,不需要return语句。在finally块中,Java已经知道处理finally块后要做什么:要么继续异常处理,要么跳转到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;
}
因此,在代码段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中,每个块中都会抛出一个异常。由于此异常未被取消选中,因此必须在方法概要中指出。此外,没有return语句,该方法的返回值不是void

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

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;
    //      }
}

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

应始终执行
finally
块。这是主要原因。异常在
catch
块中抛出,但在
finally
块掩码异常中执行
return
语句。删除
finally
块或将
return
语句移出
finally

return语句无法访问。它后面的所有块都会引发异常。是的,这是有意义的。谢谢,谢谢。所以,这一切都是关于掩蔽。我们可以说,finally块将始终掩盖catch块中抛出的异常。或者,这也有一些标准。@nipkon然后考虑下面的块,每次我们运行这个块时,输出中sysout和堆栈跟踪的顺序是不同的<代码>try{System.out.println(“In try…”);int x=10/0;}catch(异常e){System.out.println(“In catch…”);e.printStackTrace();}最后{System.out.println(“In finally…”);}很抱歉,上面的评论无法让它看起来很漂亮。