What is the internal behavior of exception handling?

If there is an exception, an exception object is created.

If the exception was not handled, what happens to this exception object and what is the internal mechanism?

+4
source share
2 answers

You can check how processed exceptions are handled :

Java actually handles uncaught exceptions according to the thread in which they occur. When an uncaught exception occurs in a specific thread, Java looks for what is called an opaque exception handler, actually an implementation of the UncaughtExceptionHandler interface. The last interface has a handleException () method that the executor redefines to take appropriate measures, such as printing a stack to the console. As we will see now, we can set our own instance of UncaughtExceptionHandler to handle thrown exceptions from a specific thread or even for the whole system.

The specific procedure is as follows. When an uncaught exception occurs, the JVM does the following:

  • it calls the special private method dispatchUncaughtException () in the Thread class in which an exception occurs;
  • , 1.

enter image description here

+3

Exception AND IS THROWN, .

, Exception, . , , , ..

            e= new Exception();
            StackTraceElement[] st = e.getStackTrace();
            for(StackTraceElement s:st)
            {
                s.getLineNumber()
            }
// NO throw
0

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


All Articles