C# tutorials > Core C# Fundamentals > Exception Handling > How do you handle multiple exceptions in a single `catch` block (exception filters)?
How do you handle multiple exceptions in a single `catch` block (exception filters)?
catch
block using exception filters. Exception filters provide a way to conditionally execute a catch
block based on the specific characteristics of the exception being thrown. This allows for more granular and targeted exception handling compared to simply catching a base exception type.
Basic Syntax and Example
FormatException
and OverflowException
separately using exception filters. The when
keyword introduces the exception filter, which checks the exception's Message
property to determine if the catch
block should execute. If the condition in the when
clause evaluates to true
, the corresponding catch
block is executed. The final catch
block catches any other Exception
types that aren't already handled.
csharp
try
{
// Code that might throw exceptions
Console.WriteLine("Enter a number:");
string input = Console.ReadLine();
int number = int.Parse(input);
Console.WriteLine($"You entered: {number}");
}
catch (FormatException ex) when (ex.Message.Contains("Input string was not in a correct format."))
{
Console.WriteLine("Error: Invalid input format. Please enter a valid number.");
}
catch (OverflowException ex) when (ex.Message.Contains("Value was either too large or too small"))
{
Console.WriteLine("Error: The number you entered is too large or too small.");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
Concepts Behind the Snippet
catch
blocks. Instead of simply catching a specific exception type and handling it generically, you can examine the properties of the exception object and only execute the catch
block if certain conditions are met. This enhances the precision and flexibility of your error handling strategy, enabling you to tailor your response to the specific circumstances of each exception. The when
keyword allows you to add a boolean expression that determines whether the catch
block will be executed.
Real-Life Use Case Section
FileNotFoundException
), or its content might be corrupted leading to parsing errors (IOException
or custom exception). Using exception filters, you can handle missing files differently from corrupted files, even if they both result in exceptions during the file reading process. For example, if the file is missing, you might try to create a default file. If the file is corrupted, you might log the error and prompt the user to fix the file.
csharp
try
{
string content = File.ReadAllText("data.txt");
// Process the content
}
catch (FileNotFoundException ex) when (ex.Message.Contains("data.txt"))
{
Console.WriteLine("File not found. Creating a default file...");
File.WriteAllText("data.txt", "Default Data");
}
catch (IOException ex) when (ex.Message.Contains("corrupted") || ex.Message.Contains("invalid format"))
{
Console.WriteLine("The file is corrupted. Please check the file.");
// Log the error details
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
Best Practices
Interview Tip
catch (ExceptionType ex) when (condition)
) and provide examples of their use. Mention that while powerful, they should be used judiciously to maintain code readability and avoid excessive complexity.
When to use them
SqlException
, you might use exception filters to handle different SQL error codes (e.g., connection errors, constraint violations). This allows you to provide more specific error messages and take appropriate actions based on the exact cause of the exception. Another valid scenario is when you have a common library and you want to add a specific treatment, based on context, without modifying the library.
Memory footprint
Alternatives
catch
blocks, each catching a specific exception type. However, this can lead to code duplication if you need to perform similar actions for multiple exception types. Another alternative is to re-throw the exception within the catch
block after performing some initial processing, but this can make the code more difficult to follow. Using a specific exception type, instead of generic exception with filters, is also a valid approach.
Pros
catch
blocks.
Cons
FAQ
-
Can I use exception filters with custom exceptions?
Yes, you can use exception filters with custom exceptions. Just make sure your custom exception has properties that can be used in the filter condition. -
Are exception filters supported in all versions of C#?
Exception filters were introduced in C# 6.0, so they are not supported in earlier versions. -
Can I use multiple conditions in an exception filter?
Yes, you can use multiple conditions in an exception filter using logical operators like&&
(AND) and||
(OR).