Once the exception is thrown, the runtime begins searching for a
suitable Catch clause that is part of this Try statement and matches the type of the exception
as well as possible. As the first Catch clause is the one that fits best, the runtime will immediately
begin executing the statements in this Catch block. We could have left off the declaration
of the exception variable x in the Catch clause and only declared the type, but we wanted to
demonstrate that exception objects produce a nice stack trace that can be useful during
debugging.
The second Catch clause will catch exceptions of the general Exception type. Should
the code in the Try block throw an exception derived from System.Exception other than
ArgumentOutOfRangeException, then this Catch block would handle it. Multiple Catch clauses
associated with a single Try block must be ordered such that more specific exception types are
listed first. The compiler won??™t compile code where more general Catch clauses are listed
before more specific Catch clauses. You can verify this by swapping the order of the first two
Catch clauses in the previous example.
CHAPTER 8 n EXCEPTION HANDLING 135
And finally (no pun intended), there is the Finally block. No matter how the Try block is
exited, the Finally block will always execute.
Pages:
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233