我有以下两个代码片段,想知道是什么让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'块。我详细查看了以下问题,但它们没有提供任何具体原因。