Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does throwing an Exception have to cause the program to terminate

Does throwing an Exception have to cause the program to terminate?

I think its no, I just want to make sure

like image 441
Stephanie Dente Avatar asked Dec 08 '22 00:12

Stephanie Dente


2 Answers

It depends on the thread where the exception is thrown, and on the other threads running on the same time in the application.

An uncaught exception terminates the thread where it was thrown. If the rest of the threads are only daemon threads, then yes, the application will be terminated.

According to Thread.setDaemon(boolean) documentation:

The Java Virtual Machine exits when the only threads running are all daemon threads.

like image 156
Eyal Schneider Avatar answered Dec 09 '22 13:12

Eyal Schneider


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.

like image 26
Sodium Hydroxide Avatar answered Dec 09 '22 13:12

Sodium Hydroxide