13.2. C# exception hierarchy

Like Java, all exception classes are subclasses of one special exception class. [8] In Java, it is java.lang.Exception; [9] in C# the grandfather of all exception classes is System.Exception.

[8] C++ developers will find this a new idea. In C++, anything can be an exception – there is no rule to say that exceptions must be instances of any special class.

[9] Which is in turn a direct subclass of java.lang.Throwable. For Java, Throwable is a direct subclass of java.lang.Object. From Throwable, we have java.lang.Error and java.lang.Exception. C# does not differentiate between exceptions and errors.

Figure 13.4 shows some significant predefined exception classes in C#.

Figure 13.4. System.Object and some C# exception classes. Shaded exception classes are of the namespace System.IO. All other classes shown here are of the System namespace.


Table 13.1 briefly describes some of the predefined exceptions. It should be evident from their names what each exception does.

Table 13.1. Some common exceptions and their descriptions
Exception classComments
ApplicationExceptionThrown 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.
ArgumentExceptionThrown 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.
ArgumentNullExceptionThrown when a method is invoked and at least one of the passed arguments is a null reference which should not be null.
ArithmeticExceptionBase class for DivideByZeroException NotFiniteNumberException, and OverflowException.
DirectoryNotFoundExceptionThrown when part of a file or directory cannot be found.
FileNotFoundExceptionThrown on an attempt to access a file that does not exist.
IndexOutOfRangeExceptionThrown 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.
InvalidCastExceptionThrown for invalid casting or explicit conversion. Similar to java.lang.ClassCastException.
IOExceptionBase 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.
NullReferenceExceptionThrown when there is an attempt to dereference a null object reference. Similar to the infamous java.lang.NullPointerException.
OutOfMemoryExceptionThrown when there is not enough memory to continue the execution of a program. Similar to java.lang.OutOfMemoryError. [1]
SystemExceptionThrown 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

[1] Unlike Java, C# does not differentiate between errors and exceptions, hence the name OutOfMemoryException.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.136