Exception Types

Understand the Exception class hierarchy, its key properties, and common runtime error types in .NET.

An exception is an object that wraps error details. When an error occurs, the runtime creates and throws an object of a specific exception type.

The Exception class

The base class of all exceptions in .NET is the System.Exception class. It defines several properties that help us understand what went wrong.

  • Message: A text description of the error.

  • StackTrace: A string showing the method call sequence that led to the exception.

  • Source: The name of the application or object that caused the error.

  • InnerException: If an exception was triggered by another exception, this property holds the original error.

  • TargetSite: The method name that threw the current exception.

The following example demonstrates these properties.