Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the base class for all exceptions in C#?
In C#, the base class for all exceptions is System.Exception. This is the root class from which all exception types derive, providing a common structure and functionality for error handling throughout the .NET framework.
The System.Exception class has two primary derived classes: System.SystemException for system-generated exceptions and System.ApplicationException for application-specific exceptions. However, Microsoft now recommends deriving custom exceptions directly from System.Exception or its appropriate subclasses rather than from System.ApplicationException.
Exception Hierarchy
System.Exception Properties
The System.Exception class provides several key properties that all exceptions inherit −
public class Exception {
public string Message { get; } // Error description
public Exception InnerException { get; } // Nested exception
public string StackTrace { get; } // Call stack information
public string Source { get; set; } // Application/object name
}
Common System Exceptions
The following table shows common exceptions that derive from System.SystemException −
| Exception Class | Description |
|---|---|
| System.NullReferenceException | Thrown when attempting to access a null object reference |
| System.IndexOutOfRangeException | Thrown when an array index is outside the bounds of the array |
| System.DivideByZeroException | Thrown when attempting to divide by zero |
| System.InvalidCastException | Thrown when an invalid type conversion is attempted |
| System.ArgumentException | Thrown when an invalid argument is passed to a method |
| System.IO.IOException | Thrown when an I/O operation fails |
| System.OutOfMemoryException | Thrown when insufficient memory is available |
| System.StackOverflowException | Thrown when the execution stack overflows |
Example
Here's an example demonstrating exception handling with the base Exception class −
using System;
class Program {
public static void Main() {
try {
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[5]); // This will throw IndexOutOfRangeException
}
catch (Exception ex) {
Console.WriteLine("Exception Type: " + ex.GetType().Name);
Console.WriteLine("Message: " + ex.Message);
Console.WriteLine("Base Type: " + ex.GetType().BaseType.Name);
}
try {
string str = null;
Console.WriteLine(str.Length); // This will throw NullReferenceException
}
catch (Exception ex) {
Console.WriteLine("\nException Type: " + ex.GetType().Name);
Console.WriteLine("Message: " + ex.Message);
Console.WriteLine("Is SystemException: " + (ex is SystemException));
}
}
}
The output of the above code is −
Exception Type: IndexOutOfRangeException Message: Index was outside the bounds of the array. Base Type: SystemException Exception Type: NullReferenceException Message: Object reference not set to an instance of an object. Is SystemException: True
Creating Custom Exceptions
When creating custom exceptions, derive directly from System.Exception or an appropriate subclass −
using System;
public class CustomException : Exception {
public CustomException() : base() { }
public CustomException(string message) : base(message) { }
public CustomException(string message, Exception innerException)
: base(message, innerException) { }
}
class Program {
public static void Main() {
try {
throw new CustomException("This is a custom exception");
}
catch (Exception ex) {
Console.WriteLine("Caught: " + ex.GetType().Name);
Console.WriteLine("Message: " + ex.Message);
Console.WriteLine("Base class: " + ex.GetType().BaseType.Name);
}
}
}
The output of the above code is −
Caught: CustomException Message: This is a custom exception Base class: Exception
Conclusion
System.Exception is the base class for all exceptions in C#, providing essential properties and methods for error handling. All built-in and custom exceptions ultimately derive from this class, creating a consistent exception handling model throughout the .NET framework.
