3.14. The Exception Class Hierarchy

An exception must represent either an Exception class object or an object of a class derived from Exception. The class framework provides many predefined exception classes. Alternatively, we can derive our own exception classes.

Consider the member function find(). It takes two parameters: a string array and a string item. If either or both arguments are null, find() throws the predefined ArgumentNullException:

 public static bool find( string [] table, string item ) { if ( table == null || item == null ) { Exception e = new ArgumentNullException(); if ( table == null ) { e.Source = "Argument One"; if ( item == null ) e.Source += " and Argument Two"; } else e.Source = "Argument Two"; throw e; } // ... return ...

Get C# Primer: A Practical Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.