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
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?
解决方案
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.