C# Reference ยท Exceptions and Diagnostics
The Exception class
Reference for System.Exception in C#: Message, InnerException, StackTrace, Data, HResult, exception hierarchy, ToString output and how to catch and wrap exceptions.
System.Exception is the base class of every exception in .NET. When code throws an exception, the runtime creates an object of a type derived from Exception, fills in the call stack and unwinds until a matching catch block is found. The object carries the information you need to understand the failure: a message, an optional inner exception that caused it, and the stack trace.
You normally catch the most specific types you can handle, and throw specific types too (ArgumentNullException, InvalidOperationException, your own subclass). Catching plain Exception is reserved for top-level handlers that log and exit, or for cleanup code that rethrows.
try { Risky(); } catch (Exception e) { Console.WriteLine(e.Message); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class Exception : ISerializable { public Exception(); public Exception(string? message); public Exception(string? message, Exception? innerException); public virtual string Message { get; } public Exception? InnerException { get; } public virtual string? StackTrace { get; } public virtual string? Source { get; set; } public virtual IDictionary Data { get; } public int HResult { get; set; } public MethodBase? TargetSite { get; } public Exception GetBaseException(); }
| Member | Description |
|---|---|
Message |
Human-readable description. For built-in exceptions it comes from the runtime resources; for your own, from the constructor argument. |
InnerException |
The exception that caused this one, or null. Pass it when wrapping so the original is not lost. |
StackTrace |
Text of the call stack at the throw point. null until the exception has been thrown. |
Data |
A dictionary for extra details. Keys and values must be serializable in some contexts, so keep them simple. |
HResult |
A numeric code, mostly used for interop and I/O errors. |
GetBaseException() |
Follows InnerException links to the root cause. |
ToString() |
Type, message, inner exceptions and stack trace in one string, suited to logs. |
Usage notes
- Throw specific types. Never throw
Exceptionitself from a library; callers cannot catch it selectively. - Order catch blocks from specific to general. The compiler reports an error (CS0160) if a general catch comes before a more specific one.
- Keep the inner exception when wrapping.
new MyException("...", inner)preserves the chain;throw new MyException("...")throws away the cause. - Rethrow with
throw;, notthrow e;, so the original stack trace stays intact. Seethrow. - Do not use exceptions for normal control flow. Throwing and catching is far slower than a branch; use
TryParse-style methods when failure is expected. - Do not catch
Exceptionand swallow it. Empty catch blocks hide bugs. Log, rethrow or handle a specific failure. - Some exceptions cannot be caught.
StackOverflowExceptionends the process, and an unhandled exception terminates it as well. - Prefer
ToString()for logs, becauseMessagealone omits the stack and the inner exceptions. - Exceptions derived from
SystemExceptionare thrown by the runtime; ones fromApplicationExceptionare a legacy base class you should not derive from.