System.Exception Class
Represents errors that occur during execution. The Exception class is the base class for all exception classes in C#.
Inheritance Hierarchy
Object
Exception
Syntax
public class Exception : Object
Description
When an error occurs in a block of code, the common language runtime (CLR) creates an instance of an exception class. This exception object contains information about the error, including a detailed description of what caused the error and the stack trace at the point when the error occurred. When an exception is thrown, the CLR searches for code that can handle the exception. If no handler is found, the program terminates.
The Exception class is the base class for all exceptions. You can throw an Exception object directly, but it's generally better to throw a more specific exception type derived from Exception. This allows you to catch specific exceptions and handle them appropriately.
Fields
The Exception class has protected fields that are used to store information about the exception. These fields are not directly accessible but are used internally.
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. |
Properties
| Name | Description |
|---|---|
Message |
Gets an String that describes the error. |
InnerException |
Gets the Exception instance that caused the current exception. |
StackTrace |
Gets a string representation of the frames in the call stack at the time the current exception was thrown. |
HelpLink |
Gets or sets a link to the help file associated with this exception. |
Source |
Gets or sets the name of the application or the object that causes the error. |
Data |
Gets a collection of key/value pairs that provide additional user-defined information about the exception. |
Methods
| Name | Description |
|---|---|
ToString() |
Overrides the default behavior to return a string representation of the exception. |
Equals(object obj) |
Determines whether the specified object is equal to the current object. |
GetHashCode() |
Serves as the default hash function. |
GetType() |
Gets the type of the current instance. |
Example
This example demonstrates how to throw and catch a DivideByZeroException, which inherits from Exception.
using System;
public class ExceptionExample
{
public static void Main(string[] args)
{
try
{
int numerator = 10;
int denominator = 0;
int result = numerator / denominator; // This will throw a DivideByZeroException
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
}
catch (Exception ex) // Catches any other exception
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
finally
{
Console.WriteLine("Execution finished.");
}
}
}