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#.
Table 13.1 briefly describes some of the predefined exceptions. It should be evident from their names what each exception does.
Exception class | Comments |
---|---|
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. [1] |
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 |
[1] Unlike Java, C# does not differentiate between errors and exceptions, hence the name OutOfMemoryException.
3.140.197.136