-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特定处理”消息,因此您已到达“catch”块。然后,应用程序退出,出现异常,因为您*抛出异常*。 – David
@David我希望执行Exception块中的代码。 – newToScala