Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a java exception terminate the whole java application?

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 test(){
    File fileDir=new File(sourceDataDir);
    if(fileDir.exists()){
        File[] files = fileDir.listFiles();
        for(int index=0 ; index<files.length ; index++){
            System.out.println("index = "+index);
            File file = files[index];
            if(index == 1)//delete a file to cause a FileNotFoundException
                file.delete();
            try {
                BufferedReader in = new BufferedReader(new FileReader(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

I delete a file to cause a FileNotFoundException manually. I used to think that the whole application will terminate when the exception happened. But in fact, the application will continue reading the remaining files. So, my question is, in what condition an exception will cause the whole application to be terminated?

like image 487
wuchang Avatar asked Sep 04 '13 07:09

wuchang


People also ask

Does an exception terminate the program?

Because there is no code to handle the exception explicitly, the program terminates by printing specific information of the situation. Now, imagine a situation where there are thousands of line of code. Such a minimal message does not provide enough information to pinpoint exactly where the exception occurred.

Does an exception always causes program termination?

No, it does not have to cause it to terminate. You could catch the exception and do something useful with it, like show a message to the user that an error occurred and why.

What happens when Java throws an exception?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.


2 Answers

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.

like image 146
Holger Avatar answered Oct 14 '22 17:10

Holger


Here's the deal with exceptions. They are of 2 types : Checked and Unchecked.

The Checked exceptions (all exceptions that are NOT subclasses of RuntimeException) must be handled in a try-catch or the code won't compile, like in the OPs question of FileNotFoundException. It's because of this forced handling that these exceptions usually do not terminate the app unless they are thrown up to the calling methods all the way up to the main() method, where they would stop the thread where they occurred.

The Unchecked exceptions (all exceptions that are subclasses of RuntimeException) need not be handled as these are from programming/data flaws (think divide by zero exception or trying to access a member of a null object etc.). These usually rip through the app and crash the thread they appear on, which could be your program on a single threaded execution.

like image 39
shazwashere Avatar answered Oct 14 '22 15:10

shazwashere