【发布时间】: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