Information Technology Reference
In-Depth Information
The catch Clauses Section
The
catch
clauses section can contain multiple
catch
clauses. Figure 11-4 shows a summary
of the
catch
clauses section.
Figure 11-4.
Structure of the catch clauses section of a try statement
When an exception is raised, the list of
catch
clauses is searched in order, and the first
catch
clause that matches the type of the exception object is executed. Because of this, there
are two important rules in ordering the
catch
clauses. They are the following:
The spec ic
catch
clauses must be ordered with the most specific exception types first,
progressing to the most general. For example, if you declare an exception class derived
from
NullReferenceException
, the
catch
clause for your derived exception type should
be listed before the
catch
clause for
NullReferenceException
.
If there is a general
catch
clause, it must be the last clause—after all specific
catch
clauses.