public class ExceptionTest {
public static void main(String[] args) {
ExceptionTest et = new ExceptionTest();
try {
et.testMethod();
} catch (Exception e) {
e.printStackTrace();
}
}
public int testMethod() {
try {
throw new Exception();
}finally {
return 4;
}
}
上面的代碼工作正常,但是當我改變的testMethod()
返回類型爲void和轉產return 4;
到System.out.println("some print msg");
導致編譯問題。異常處理沒有趕上嘗試,但最終
任何人都可以給解決方案,爲什麼它給編譯錯誤?
這個問題似乎寧可爲什麼它是*不*在第一種情況下給了一個錯誤,當你清楚不捕捉異常或聲明它拋出。 – Gendarme
在Java8中工作得很好... – Paul
@Paul我在Java 8中遇到了錯誤。 – Gendarme