I used to think that when an exception happened, the whole java application will be terminated. For example, I write a test function to test my idea.
public void
No. Exception handlers
of java handles the abnormal execution of the program
There are two main types of exceptions:
It indicates the system is working properly. You must catch the exception or it won't compile. It will never terminate the application. If you want to terminate anyway, you have to call System.exit(ERROR_NUMBER) in the catch block or throw a runtime exception.
Indicates a system error (eg. application server misconfiguration). You don't have to catch it. If you don't catch it, it terminates the application or if you write a J2EE application the AS might handle it and continue your app.
So, my question is, in what condition, a exception will cause the whole application to be terminated?
See docs for details on Exceptions. As for your problem FileNotFoundException is a checked Exception which means you need to handle it. Now you have to options
In case 1 your java process will continue till it reaches the end(assuming no runtime Exceptions/errors occur).
In case 2 if you do not catch your FileNotFoundException even in the parent(calling function) and just throw it again and continue to do so, the exception will finally land up in the main() method. If even your main() method throws this exception JVM will simply shut down.
Update for clarifying your comments:
Catching an Exception is independent of whether Excpetion is catched ot not. Inc ase of uncahed Exception you can still catch it and let the program proceed. But that is not recommended because by definition of uncached Exception(which should not happen at all) you are not suppose to recover if uncached Exception occurs.
Consider following simple example
public static void main(String args[]) {
try {
String s = null;
System.out.println(s.length());
} catch (Exception e) {
System.out.println("Catch runtime exception but not quite sure what to do with it");
}
System.out.println("Reached here even after uncatched Exception");
}
output is
Catch runtime exception but not quite sure what to do with it
Reached here even after uncatched Exception
So basically whenever Exception occurs if you do not catch it at any level from the point of origin, it will eventually propagate to main() and JVM will eventually shut down. If you do catch it(irrespective of catched or uncatched Exception) your program will proceed(output may not be as expected in case of uncatched Exceptions) and terminate.
If an exception is not caught with catch
, the thread in which the exception occurred will be terminated. If no non-daemon threads remain the JVM will terminate. That's the only way how an exception might terminate a JVM. If you catch an exception it will never cause a JVM termination.
my question is ,in what condition ,a exception will cause the whole application to be terminated?
It won't ever. Only System.exit() causes the whole program to terminate (and a JVM crash)
Think of an Exception and like a powerful break;
which can break out of methods. A break could result in a loop exiting and if the last loop in the only non daemon thread, the program will exit. But it doesn't cause it, even though it might be the last thing you see before you program dies.