Throws an exception so that the program terminates

Does the exception throw a program interrupt?

I think not, I just want to make sure that

+4
source share
5 answers

This depends on the thread in which the exception is thrown, as well as for other threads running at the same time in the application.

An uncaught interrupt completes the thread into which it was thrown. If the remaining threads are only daemon threads, then yes, the application will be terminated.

According to the documentation of Thread.setDaemon (boolean) :

The Java virtual machine shuts down when all threads are executed only by daemon threads.

+5
source

In Java and .NET, if you do not handle the exception, this will most likely cause your program to terminate.

A simple exception excludes the program as such. This is what happens after his throw, which determines what will happen.

+3
source

No, this should not lead to its completion. You can catch the exception and do something useful with it, for example, show a message to the user that an error has occurred and why.

+3
source

Failure to catch an exception is likely to lead to the termination of the program, but the action of throwing one will not occur. At least any application should have some kind of last protection in order to catch all unhandled exceptions and handle them (even if handling them means, at least for some, throwing them outside the application and ending because something is external application expects this).

+2
source

Only "Unhandled Exceptions" will cause your program to crash. For exception handling you use the following form

try { // May Throw ApocalypseException functionThatMightBlowUpTheWorld(); } catch (ApocalypseException e){ System.err.println("We accidentally almost blew up the world because: "); System.err.println(e.message); } 
+2
source

Source: https://habr.com/ru/post/1332826/


All Articles