I'm trying to catch a specific exception and handle it, and then throw a generic exception which executes code to handle any exception. How is this accomplished? This snippet doesn't catch the Exception
and so the output is
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
The snippet:
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");
}
}
}