dotnet
.NET / C# Exceptions
Exception types thrown by the .NET runtime and standard library, used across C#, F#, and VB.NET applications.
20 codes
references learn.microsoft.com/en-us/dotnet/api/system.exceptionlearn.microsoft.com/en-us/dotnet/standard/exceptions/
· All codes 20 codes
- ArgumentException ArgumentException Thrown when a method receives an argument that is not valid. The Message property describes the invalid argument and the ParamName property names it. Base class of ArgumentNullException and ArgumentOutOfRangeException.
- ArgumentNullException ArgumentNullException Thrown when a null reference is passed to a method parameter that does not accept it. Indicates a programming error at the call site. Use ArgumentNullException.ThrowIfNull() (.NET 6+) or explicit null guards at the top of methods.
- ArgumentOutOfRangeException ArgumentOutOfRangeException Thrown when the value of an argument is outside the allowable range defined for that parameter. Commonly seen with index arguments that are negative or exceed a collection's bounds.
- DivideByZeroException DivideByZeroException Thrown when integer division or modulo by zero is attempted. Floating-point division by zero does not throw; it produces Infinity or NaN per IEEE 754. Guard integer divisors before the operation.
- FileNotFoundException FileNotFoundException Thrown when an attempt to access a file that does not exist on disk fails. Check that the path is correct, the file has not been deleted, and the process has permission to read it. Use File.Exists() to guard access when absence is a normal condition.
- FormatException FormatException Thrown when the format of a string argument is invalid for a parse or conversion operation. Common with int.Parse(), DateTime.Parse(), and Guid.Parse(). Use the TryParse() variants to avoid the exception when input may be malformed.
- HttpRequestException HttpRequestException Thrown by HttpClient when an HTTP request fails due to an underlying connection problem, DNS failure, or TLS error. For non-success HTTP status codes, check response.EnsureSuccessStatusCode() or response.IsSuccessStatusCode instead. The StatusCode property (.NET 5+) carries the HTTP status when available.
- IndexOutOfRangeException IndexOutOfRangeException Thrown when an array or collection is accessed with an index that is outside its valid range (less than zero or greater than or equal to its Length). Prefer ArgumentOutOfRangeException in your own APIs; this type is reserved for the runtime.
- InvalidOperationException InvalidOperationException Thrown when a method call is invalid for the object's current state. Common examples: calling MoveNext() on a disposed enumerator, modifying a collection while iterating it, or accessing a UI control from a non-UI thread.
- KeyNotFoundException KeyNotFoundException Thrown when the key specified for accessing a dictionary element is not found. Use Dictionary.TryGetValue() or ContainsKey() to avoid the exception when the key may legitimately be absent.
- NotImplementedException NotImplementedException Thrown when a method or operation has not been implemented. Commonly used as a placeholder during development or in abstract class stubs. Should not appear in production code paths.
- NotSupportedException NotSupportedException Thrown when a method or operation is not supported in the current context or implementation. Unlike NotImplementedException, this signals a deliberate design decision — the operation will never be supported — rather than incomplete work.
- NullReferenceException NullReferenceException Thrown when code attempts to dereference a null object reference. The most common exception in C# — check that the object was initialized and is not null before accessing its members. Use the null-conditional operator (?.) or null checks to guard access.
- ObjectDisposedException ObjectDisposedException Thrown when a method is called on an object that has already been disposed (had Dispose() called on it). Commonly seen with streams, database connections, and HttpClient instances. Ensure the object's lifetime exceeds all usages.
- OutOfMemoryException OutOfMemoryException Thrown when the runtime cannot allocate enough memory to continue execution. May indicate a genuine memory shortage, a large object heap fragmentation issue, or a memory leak. Profile with a memory profiler and check for objects kept alive longer than needed.
- OverflowException OverflowException Thrown in a checked arithmetic context when the result of an operation exceeds the range of the data type. Only thrown inside checked {} blocks or with the /checked compiler option; unchecked arithmetic wraps silently.
- StackOverflowException StackOverflowException Thrown by the .NET runtime when the call stack overflows, typically due to unbounded or very deep recursion. Cannot be caught in user code in modern .NET; the process terminates. Review recursive methods for missing base cases.
- TaskCanceledException TaskCanceledException Thrown when a Task is cancelled via a CancellationToken. Derives from OperationCanceledException; catch the base type when you want to handle any cancellation. Common when await-ing HttpClient calls or operations that observe a CancellationToken.
- TimeoutException TimeoutException Thrown when the time allotted for a process or operation has expired before the operation completed. Common with network calls, database queries, and synchronization primitives. Review timeout settings and consider using CancellationToken for cooperative cancellation.
- UnauthorizedAccessException UnauthorizedAccessException Thrown when the operating system denies access because of an I/O error or a specific type of security error, such as insufficient file system permissions or attempting to write to a read-only file.