Summary

Represents errors that occur during application execution.

System.SystemException

Namespace: System

Assembly: mscorlib (in mscorlib.dll)

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 the current exception.

Properties

Name Description
Data Gets a collection of key/value pairs that provide additional user-defined information about the exception.
HelpLink Gets or sets a link to the help file that is associated with this exception.
HResult Gets or sets HRESULT, which is a numeric value that is assigned to a specific exception.
InnerException Gets a reference to the inner exception that is the cause of the current exception.
Message Gets a message that describes the current exception.
Source Gets or sets the name of the application or the object that causes the error.
StackTrace Gets a string representation of the immediate frames of the call stack.
TargetSite Gets the method that throws the current exception.

Methods

Name Description
CreateObjRef(Type requestedType) Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetBaseException() When overridden in a derived class, returns the Exception that is the root cause of the current exception.
GetHashCode() Serves as the default hash function.
GetObjectData(SerializationInfo info, StreamingContext context) Sets the SerializationInfo object with information about the exception.
GetType() Gets the runtime type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
ToString() Returns a string that represents the current exception.

Remarks

The Exception class is the base class for all exceptions in the .NET Framework. It provides the fundamental properties and methods for handling exceptions.

When an exception occurs, the common language runtime (CLR) creates an object derived from the Exception class. This object contains information about the exception, including its type, a message describing the error, the call stack at the time of the error, and potentially an inner exception.

You can catch exceptions using a try-catch block. The catch block specifies the type of exception to catch. If the type of the exception caught matches the type specified in the catch block, the code within the catch block is executed.

.NET Example:
try
{
    // Code that might throw an exception
    int result = 10 / 0; 
}
catch (DivideByZeroException ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
    // Log the exception details
    Console.WriteLine(ex.StackTrace);
}
catch (Exception ex) // Catch any other exceptions
{
    Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}