Section 16.3. Searching for an Exception Handler


16.3. Searching for an Exception Handler

When your program encounters an exceptional circumstance, such as running out of memory, it throws an exception. Exceptions must be handled before the program can continue.

If the currently running function does not handle the exception, the current function terminates and the calling function gets a chance to handle the exception (this is called " unwinding the stack"). If none of the calling functions handles it, the exception ultimately is handled by the Common Language Runtime (CLR), which abruptly terminates your program.

In other words, if function A calls function B, and function B calls function C, these function calls are all placed on the stack. When a programmer talks about "unwinding the stack," what is meant is that you back up from C to B to A, as illustrated in Figure 16-1.

Figure 16-1. Unwinding the stack

If you must unwind the stack from C to B to A to handle the exception, when you are done, you are in A; there is no automatic return to C.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net