.NET Core Concepts

Exception Handling in .NET Core

Exception handling is a fundamental aspect of robust application development. It allows you to gracefully manage runtime errors that occur during the execution of your .NET Core applications, preventing unexpected crashes and providing a better user experience.

What are Exceptions?

An exception is an event that occurs during program execution that disrupts the normal flow of instructions. When an exception occurs, the program's execution is suspended and the system searches for code that can handle the exception. If no handler is found, the program terminates.

The Exception Class Hierarchy

.NET Core exceptions are represented by classes that derive from the System.Exception base class. Key exception classes include:

Throwing Exceptions

You can throw exceptions using the throw keyword when you detect an error condition that your code cannot handle.

Example: Throwing a ArgumentOutOfRangeException


public void SetAge(int age)
{
    if (age < 0 || age > 120)
    {
        throw new ArgumentOutOfRangeException(nameof(age), "Age must be between 0 and 120.");
    }
    this.age = age;
}
                

Handling Exceptions: try-catch-finally

The try-catch-finally block is the primary mechanism for handling exceptions:

Example: Using try-catch-finally


try
{
    // Code that might throw an exception
    string filePath = "data.txt";
    string content = File.ReadAllText(filePath);
    Console.WriteLine(content);
}
catch (FileNotFoundException ex)
{
    Console.WriteLine($"Error: The file was not found. {ex.Message}");
}
catch (IOException ex)
{
    Console.WriteLine($"An I/O error occurred. {ex.Message}");
}
catch (Exception ex) // Catch-all for any other exceptions
{
    Console.WriteLine($"An unexpected error occurred. {ex.Message}");
}
finally
{
    // Code that always runs, e.g., closing a file or releasing a connection
    Console.WriteLine("Cleanup operations performed.");
}
                

Exception Filters

Exception filters allow you to specify conditions for when a catch block should be executed. This can be useful for logging or conditional handling without "catching" the exception definitively.

Example: Using an Exception Filter


try
{
    // ... code that might throw ...
}
catch (Exception ex) when (ex.Message.Contains("specific error"))
{
    Console.WriteLine($"Handling a specific error: {ex.Message}");
}
                

Best Practices for Exception Handling