Java 在catch块内引发相同的异常
我有以下两个代码段,我想知道是什么使java编译器(在带有java 7的Eclipse中)显示第二个代码段的错误,为什么不显示第一个代码段的错误 以下是代码片段: 片段1Java 在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
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”块
我详细研究了以下问题,但没有提供任何具体原因
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…”);}很抱歉,上面的评论无法让它看起来很漂亮。