#860 – Exceptions Bubble up the Call Stack

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.




About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #860 – Exceptions Bubble up the Call Stack

  1. Pingback: Dew Drop – June 6, 2013 (#1,562) | Alvin Ashcraft's Morning Dew

  2. Steven says:

    Your explanation & diagram are really good. Thanks, Sean!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: