Like Java, all exception classes are subclasses of one special exception class. In Java, it is java.lang.Exception; in C# the grandfather of all exception classes is System.Exception.
Table 13.1. Some common exceptions and their descriptions
|
ApplicationException | Thrown by a user program (not by the CLR) when a non-fatal error occurs. If you are writing your own exception classes, derive them from this class. Although this exception extends System.Exception, it does not add new functionality ?rather, its main purpose is to differentiate user-written application exceptions from those thrown by the CLR. |
ArgumentException | Thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. Instances of this exception should carry a meaningful error message describing the invalid argument, as well as the expected range of values for the argument. |
ArgumentNullException | Thrown when a method is invoked and at least one of the passed arguments is a null reference which should not be null. |
ArithmeticException | Base class for DivideByZeroException NotFiniteNumberException, and OverflowException. |
DirectoryNotFoundException | Thrown when part of a file or directory cannot be found. |
FileNotFoundException | Thrown on an attempt to access a file that does not exist. |
IndexOutOfRangeException | Thrown when an attempt is made to access an element of an array with an index outside the bounds of the array. Similar to java.lang.ArrayIndexOutOfBoundsException. |
InvalidCastException | Thrown for invalid casting or explicit conversion. Similar to java.lang.ClassCastException. |
IOException | Base class for exceptions thrown while accessing information using streams, files, and directories. Subclasses of IOException include DirectoryNotFoundException, EndOfStreamException, FileNotFoundException, FileLoadException, and PathTooLongException. I/O exceptions are in the System.IO namespace. |
NullReferenceException | Thrown when there is an attempt to dereference a null object reference. Similar to the infamous java.lang.NullPointerException. |
OutOfMemoryException | Thrown when there is not enough memory to continue the execution of a program. Similar to java.lang.OutOfMemoryError. |
SystemException | Thrown by the CLR when errors occur that are non-fatal and recoverable by user programs. Although this exception extends System.Exception, it does not add new functionality ?rather, its main purpose is to differentiate exceptions thrown by the CLR and user-written application exceptions |