Mono Class Library: System NamespaceSystem.Exception Class |
See Also: Exception Members
|
All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.
This class is the base class for all Exceptions.
When an error occurs, either the system or the currently executing application reports it by throwing an Exception containing information about the error. Once thrown, an Exception is handled by the application or by the default exception handler.
Note: For a description of the exception handling model, see Partition I of the CLI Specification.Note:If an application handles exceptions that occur during the execution of a block of application code, the code is required to be placed within a try statement. Application code within a try statement is a try block . Application code that handles Exceptions thrown by a try block is placed within a catch statement, and is called a catch block . Zero or more catch blocks are associated with a try block, and each catch block includes a type filter that determines the types of Exceptions it handles.
When an Exception occurs in a try block, the system searches the associated catch blocks in the order they appear in application code, until it locates a catch block that handles the Exception. A catch block handles an exception of type T, if the type filter of the catch block specifies T or any type that T derives from. The system stops searching after it finds the first catch block that handles the Exception. For this reason, in application code, a catch block that handles a type must be specified before a catch block that handles its base types, as demonstrated in the example that follows this section. A catch block that handles Exception is specified last.
If the catch blocks associated with the current try block do not handle the Exception, and the current try block is nested within other try blocks in the current call, the catch blocks associated with the next enclosing try block are searched. If no catch block for the Exception is found, the system searches previous nesting levels in the current call. If no catch block for the Exception is found in the current call, the Exception is passed up the call stack, and the previous stack frame is searched for a catch block that handles the Exception. The search of the call stack continues until the Exception is handled or there are no more frames in the call stack. If the top of the call stack is reached without finding a catch block that handles the Exception, the default exception handler handles it and the application terminates.
Exception types support the following features:
- Human-readable text that describes the error.
Note: See Exception.Message property.- The state of the call stack when the Exception was thrown.
Note: See the Exception.StackTrace property.- When there is a causal relationship between two or more Exceptions, this information is maintained via the Exception.InnerException property.
The Base Class Library provides two types that inherit directly from Exception :
Note: Most user-defined exceptions derive from ApplicationException. For more information, see ApplicationException and SystemException .[Edit]
The following example demonstrates a catch block that is defined to handle ArithmeticException errors. This catch block also catches DivideByZeroException errors because DivideByZeroException derives from ArithmeticException, and there is no catch block explicitly defined for DivideByZeroException errors.
C# Example using System; class ExceptionTestClass { public static void Main() { int x = 0; try { int y = 100/x; } catch (ArithmeticException e) { Console.WriteLine("ArithmeticException Handler: {0}", e.ToString()); } catch (Exception e) { Console.WriteLine("Generic Exception Handler: {0}", e.ToString()); } } }The output is
Example ArithmeticException Handler: System.DivideByZeroException: Attempted to divide by zero. at ExceptionTestClass.Main()
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0