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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.