Pre-built Exceptions In .NET

Written by Krunal Shah /

TimeoutExceptionHttpExceptionFormatExceptionFileNotFoundExceptionSocketExceptionAggregateExceptionHttpRequestExceptionOperationCanceledExceptionAccessViolationExceptionOutOfMemoryExceptionNotSupportedExceptionKeyNotFoundExceptionObjectDisposedExceptionInvalidOperationExceptionUnauthorizedAccessExceptionIndexOutOfRangeExceptionOverflowExceptionSqlExceptionAuthenticationExceptionStackOverflowExceptionNotImplementedExceptionArgumentNullExceptionDivideByZeroExceptionArgumentOutOfRangeException

Pre-built Exceptions In .NET

If you need to throw an exception in parts of your code, take a look at these things before creating a new exception!

TimeoutException

This exception is thrown when an operation times out before it can complete.

You can use this exception when working with network or database operations that have a timeout value.

HttpException

This exception is specific to ASP.NET and is thrown when an HTTP-related error occurs, such as a 404 Not Found error.

You can use this exception when handling HTTP-related errors in your web application.

FormatException

This exception is thrown when a string cannot be parsed into a specific data type.

You can use this exception when validating user input or when working with strings that need to be converted to a specific data type.

FileNotFoundException

This exception is thrown when a file cannot be found.

You can use this exception when working with files to ensure that the file exists before performing operations on it.

SocketException

This exception is specific to network programming and is thrown when a network-related error occurs, such as a connection failure.

You can use this exception when working with network programming to handle errors that may occur.

AggregateException

This exception is thrown when multiple exceptions occur during the execution of a task or operation.

You can use this exception to aggregate multiple exceptions and handle them together.

HttpRequestException

This exception is specific to ASP.NET Core and is thrown when an HTTP-related error occurs, such as a connection failure or timeout.

You can use this exception when working with HTTP requests to handle errors that may occur.

OperationCanceledException

This exception is thrown when an operation is cancelled, usually due to a cancellation token being triggered.

You can use this exception to cancel long-running operations and gracefully exit the application.

AccessViolationException

This exception is thrown when an attempt is made to access memory that is not authorized.

You can use this exception when working with low-level memory operations or when accessing unmanaged resources.

OutOfMemoryException

This exception is thrown when there is not enough memory to perform an operation.

You can use this exception when working with memory-intensive operations, such as image or video processing.

NotSupportedException

This exception is thrown when a method or feature is not supported in the current context or platform.

You can use this exception when developing platform-specific code that may not be supported on all platforms.

KeyNotFoundException

This exception is thrown when a specified key is not found in a dictionary.

You can use this exception when working with dictionaries to ensure that a key exists before attempting to access its value.

ObjectDisposedException

This exception is thrown when an object has been disposed and cannot be used anymore.

You can use this exception when working with disposable objects to ensure that the object is still valid before performing operations on it.

InvalidOperationException

This exception is thrown when an operation is not valid for the current state of an object.

You can use this exception when enforcing business rules or ensuring that certain operations are performed in a specific order.

UnauthorizedAccessException

This exception is thrown when an operation is not authorized or a user does not have permission to perform a specific action.

You can use this exception when implementing access control or security checks.

IndexOutOfRangeException

This exception is thrown when an index is outside the bounds of an array or collection.

You can use this exception when working with arrays or collections to ensure that the index is within the valid range.

OverflowException

This exception is thrown when a value is too large or too small to be represented by the data type.

You can use this exception when working with arithmetic operations that may result in an overflow.

SqlException

This exception is specific to SQL Server and is thrown when a database-related error occurs, such as a syntax error in a SQL query.

You can use this exception when working with SQL Server databases to handle errors that may occur.

AuthenticationException

This exception is specific to ASP.NET Core and is thrown when an authentication-related error occurs, such as invalid credentials or expired tokens.

You can use this exception to handle authentication errors in your application.

StackOverflowException

This exception is thrown when the stack overflows, usually due to excessive recursion.

You can use this exception when working with recursive algorithms to ensure that the recursion depth does not exceed a certain limit.

NotImplementedException

This exception is thrown when a method or feature has not been implemented yet.

You can use this exception when you need to provide a placeholder for future development work.

ArgumentNullException

This exception is thrown when a null argument is passed to a method or constructor.

You can use this exception to ensure that required arguments are not null.

DivideByZeroException

This exception is thrown when an attempt is made to divide a number by zero.

You can use this exception when working with arithmetic operations that may result in a divide by zero error.

ArgumentNullException

This exception is thrown when an argument provided to a method or constructor is invalid or out of range.

You can use this exception when validating user input or configuration settings.