#860 – Exceptions Bubble up the Call Stack
June 6, 2013 2 Comments
When you define a try/catch block, the handler defined in the catch block will catch exceptions that originate directly from the code in the try block. But the handler will also catch exceptions that originate in methods called from the code in the try block, or from code that those methods call. In short, a catch block will catch any exceptions thrown by code that executes as a result of executing the block of code in the try block. (Assuming that the exception is not caught elsewhere).
In the example below, code within a try block calls SomeMethod, which calls AnotherMethod. AnotherMethod then calls YetAnother. If an exception is thrown from the YetAnother method, the exception will bubble up the call stack until an exception handler is found. In this the handler associated with the try block around the SomeMethod call will catch the exception.