Class System.Exception

Provides a base class for all user-defined and framework-supplied exceptions.

Syntax


[SerializableAttribute]
public class Exception : object
            

Remarks

The System.Exception class is the base class for all exceptions in the .NET Framework. Exceptions are a mechanism for handling runtime errors. When an error occurs, an exception object is created and "thrown". This transfers control to a handler for that exception. If no handler is found, the program terminates.

The Exception class contains a number of properties that provide information about the error. Key properties include:

Custom exception classes should inherit from System.Exception. It is also recommended to include a constructor that takes a message string and a constructor that takes a message string and an inner exception. The [SerializableAttribute] should also be applied to custom exception classes.

Constructors

Name Description
Exception() Initializes a new instance of the Exception class.
Exception(string message) Initializes a new instance of the Exception class with a specified error message.
Exception(string message, Exception innerException) Initializes a new instance of the Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.

Fields

Name Description
InnerException Gets a reference to the inner exception that is the cause of the current exception.
Message Gets an error message that describes the current exception.
StackTrace Gets a string that contains the name of the application domain in which the current exception occurred and the names of the assemblies involved in the error and the location of the error.

Methods

Name Description
GetBaseException() When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.
ToString() Returns a string that represents the current exception.
Note: This is a base class. Most applications will not create an instance of Exception directly, but will instead create an instance of a derived class.