The following code compiles perfectly. And I believe it's because the compiler knows at compile time that the control will go to the finally block and throw the unchecked exception (which is okay and doesn't require to be handled) and it knows that all other exceptions thrown by the code prior to this point are lost. so no need to worry abt them.
try{
// DoSomething();
}catch(Exception e){
// Throw checked exception
}finally{
// Throw unchecked exception
}
example:
public class TestClass {
public static void main(String[] args) {
TestClass c = new TestClass();
try {
// Whatever
} catch (Exception e) {
throw new FileNotFoundException();
} finally {
throw new NullPointerException();
}
}
}
so far so good until I throw the unchecked exception from a method.
try{
// DoSomething();
}catch(Exception e){
// Call a method that throws a checked exception
// or just throw the checked exception from here
}Finally{
// Call a method that throw an unchecked exception
}
example:
public class TestClass {
public static void main(String[] args) {
TestClass c = new TestClass();
try {
//Whatever
} catch (Exception e) {
c.m1();
// or just throw it here
// throw new FileNotFoundException();
} finally {
c.m2();
}
}
public void m1() throws IOException {
throw new FileNotFoundException();
}
public void m2() throws RuntimeException {
throw new NullPointerException();
}
}
This code will not compile. It marks c.m1() with an error "Unhandled exception type _ " (eclipse) or "unreported exception _; must be caught or declared to be thrown" (cmd).
It's like it ignored that the finally block will throw the LAST exception (which is unchecked) and no need to worry about the one in the catch block even if it was an unhandled checked exception because they are going to be lost anyway! Knowing that m2() is DECLARED to throw specifically an unchecked exception (RuntimeException).
Does anyone have a better explanation about why there's a compilation error in the second code? Thanks :)