Exceptions



Exceptions

In order to provide a common, rich, easily programmed, and difficult to ignore way of signaling and handling errors, the .NET Framework supports structured exception handling. A caller places an exception handler on the stack at the point at which he wants to catch the error, using the try–catch syntax of his programming language. A called function wanting to signal an error creates an object of class System.Exception (or one derived from it) containing information about the error and throws it. The CLR searches up the call stack until it finds a handler for the type of exception that was thrown, at which time the stack is unwound and control transferred to the catch block, which contains the error handling code.

The class System.Exception is the base class from which all exception objects derive. It contains such basic information as a message provided by the thrower and the stack trace at which the exception took place. The class System.SystemException derives from it, and all system-provided exceptions derive from that. This allows a programmer to differentiate between system-provided and programmer-built exceptions. The system-provided exceptions in Figure were felt to be common enough to occupy the base System namespace and are covered in this volume of the .NET Framework Standard Library Annotated Reference. Many more specialized exception classes live in subordinate namespaces.

Exception Class

Meaning

BadImageFormatException

The file image of a DLL or an executable program is invalid.

CannotUnloadAppDomainException

An attempt to unload an application domain fails.

EnTRyPointNotFoundException

An attempt to load an assembly failed due to the absence of an entry method.

FieldAccessException

An illegal attempt was made to access a field inside a class.

MemberAccessException

An attempt to access a class member fails.

MethodAccessException

An illegal attempt to access a method inside a class.

MissingFieldException

An attempt to access a field that does not exist.

MissingMemberException

An attempt to access a class member that does not exist.

MissingMethodException

An attempt to access a method that does not exist.

TypeLoadException

Type-loading failure occurred.

TypeUnloadedException

There is an attempt to access an unloaded class.

UriFormatException

An invalid Uniform Resource Identifier (URI) is detected.