-2
特定の例外を捕捉して処理しようとしていて、例外を処理するコードを実行する汎用例外をスローしています。これはどのように達成されますか?このスニペットはException
をキャッチしていないので、出力がcatchブロック内で例外をスローする
Exception in thread "main" java.lang.Exception
at Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
IOException specific handling
Process finished with exit code 1
抜粋です:
import java.io.IOException;
public class Main {
public static void main(String[] args) throws Exception {
try {
throw new IOException();
} catch (IOException re) {
System.out.println("IOException specific handling");
throw new Exception();
} catch (Exception e) {
System.out.println("Generic handling for IOException and all other exceptions");
}
}
}
[catchブロック内にスローされた例外 - 再び捕捉されるのでしょうか?](http://stackoverflow.com/questions/143622/exception-thrown-inside-catch-block-will-it-be-caught-もう一度) –
これはどのように失敗するのですか? '' IOException specific handling "'というメッセージが出力されたため、あなたは 'catch'ブロックに達しました。その後、アプリケーションは例外で終了しました。なぜなら、あなたは*例外を投げたからです。 – David
@David Exceptionブロック内のコードを実行したい。 – newToScala