Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching and Throwing an Exception: What happens "under the hood"?

I am not familiar with error/exception handling. What is happening "under the hood" when an exception is caught and thrown?

I.e. what is the point of catching an exception in a try-catch block, then throwing it?

E.g:

  try {
        //Stuff
    } catch(StuffException e) {
        throw new MyException();
    }
}
like image 622
rni902 Avatar asked Oct 26 '15 15:10

rni902


People also ask

What happens when you throw an exception in a catch block?

Q #3) What happens when a catch block throws an exception? Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.

What happens when an exception is thrown?

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.

What happens when a catch handler throws an exception in Java?

If an exception is thrown inside the catch-block and that exception is not caught, the catch-block is interrupted just like the try-block would have been. When the catch block is finished the program continues with any statements following the catch block. In the example above the "System.

What happens when an exception is thrown by the main method?

- The main method should simply terminate if any exception occurs. The throws clause only states that the method throws a checked FileNotFoundException and the calling method should catch or rethrow it. If a non-checked exception is thrown (and not catch) in the main method, it will also terminate.


2 Answers

Regarding the inner workings of the exception mechanism: There is plenty of documentation on this. I'm particularly a fan of this article: http://www.javaworld.com/article/2076868/learn-java/how-the-java-virtual-machine-handles-exceptions.html

Ultra-short summary: When an exception is thrown, the jvm looks up in a table where the execution (of the init() method of the exception) continues.

For the second part of your question:

what is the point of catching an exception in a try-catch block, then throwing it?

I see some reasons for catching an exception an throwing another one:

  • You might want to catch an unchecked exception (because you know, "something bad might happen") and throw a checked one - so the caller has to handle it.

  • You want to use a custom Exception, maybe with additional information/logic

  • You're Implementing an error facade, e.g. throwing exceptions and catching them at the end in the facade.

like image 97
mish Avatar answered Oct 12 '22 20:10

mish


Your example catches something with the type of StuffException, then throws a MyException. This is done to abstract away the original exception; it might be that the original exception is an implementation detail that the thing calling it doesn't need to know about (and which needs to be able to change, so that the caller shouldn't depend on it), and the exception thrown to replace it is part of the published API, which the caller can depend on. This is the case with Hibernate, it catches SQLExceptions generated in the course of calling JDBC functions, and wraps them in Hibernate exceptions, so calling code recognizes them as being thrown from Hibernate, and calling code doesn't depend directly on the JDBC exceptions, but on Hibernate's. If JDBC changed what exceptions it throws then, once Hibernate adapted to it, users of Hibernate wouldn't have to change.

What's bad about this example is that the stacktrace information for the original StuffException gets discarded when e goes out of scope, and MyException's stacktrace starts with the location that threw the MyException, making it difficult to find the source of the actual problem. Assigning the old exception to the new exception as its cause would preserve the original stacktrace information:

try {
        //Stuff
    } catch(StuffException e) {
        MyException myException = new MyException();
        myException.initCause(e);
        throw myException;
    }
}
like image 44
Nathan Hughes Avatar answered Oct 12 '22 20:10

Nathan Hughes