C# > Advanced C# > Exception Handling > Exception Filters
Exception Filters: Logging and Conditional Handling
This snippet demonstrates how to use exception filters in C# to selectively handle exceptions based on their properties or state, without catching and re-throwing. Exception filters provide a powerful way to log exceptions or perform other side effects without altering the normal exception handling flow if the filter condition isn't met.
Code Example
The code attempts to read from a file that likely doesn't exist, triggering a The subsequent FileNotFoundException
. The catch
block uses an exception filter when (LogException(ex))
. The LogException
method is called as part of the filter condition. It simulates logging the exception. Critically, it returns false
. Because LogException
returns false
, the catch
block *does not* handle the exception. The exception continues to propagate up the call stack to the next appropriate handler. If LogException
returned true
, the catch
block *would* handle the exception.catch (Exception ex)
block catches any other exception, including the original FileNotFoundException
because the first catch block did not handle it.
using System;
using System.IO;
public class ExceptionFilterExample
{
public static void Main(string[] args)
{
try
{
// Simulate a potential file access issue
string filePath = "nonexistent_file.txt";
string content = File.ReadAllText(filePath);
Console.WriteLine(content);
}
catch (FileNotFoundException ex) when (LogException(ex))
{
// This block will only execute if LogException returns false
Console.WriteLine("File not found. An administrator has been notified.");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
Console.WriteLine("Program finished.");
}
private static bool LogException(Exception ex)
{
// Simulate logging the exception
Console.WriteLine($"Logging exception: {ex.GetType().Name} - {ex.Message}");
// In a real application, you'd log to a file or database here
// Return false to indicate that this handler should NOT handle the exception.
// Returning true would handle the exception, equivalent to catching it
return false;
}
}
Concepts Behind the Snippet
Exception filters allow you to inspect an exception without fully handling it. The when
keyword is used to specify a boolean expression that must be true for the catch
block to execute. The expression can include method calls, allowing for complex filtering logic. This approach is beneficial for logging, diagnostics, or performing other side effects without altering the exception's propagation path unless a specific condition is met.
Real-Life Use Case
Imagine an application that processes financial transactions. You might want to log all exceptions related to database connectivity issues but only handle exceptions related to insufficient funds. Exception filters would allow you to log the database exceptions for monitoring purposes without preventing the application from retrying the connection. You would then handle the insufficient funds exception specifically to notify the user.
Best Practices
Interview Tip
Be prepared to explain the benefits of exception filters over simply catching and re-throwing exceptions. Highlight their ability to perform side effects without interfering with the exception handling process if the filter condition is not met. Understand the performance implications of complex filter conditions.
When to Use Them
Use exception filters when you need to perform actions based on the exception type or properties but don't necessarily want to handle the exception in the current catch
block. This is useful for logging, diagnostics, and conditional handling of specific error scenarios.
Memory Footprint
Exception filters have a minimal memory footprint. The main overhead is the execution of the filter condition, which should be kept as efficient as possible to avoid performance degradation.
Alternatives
Alternatives to exception filters include:
Pros
Cons
FAQ
-
Can I use multiple exception filters in a single try-catch block?
No, you can only have onewhen
clause percatch
block. However, you can have multiplecatch
blocks, each with its own filter. -
What happens if the exception filter throws an exception?
If the exception filter throws an exception, the filter is considered to have returnedfalse
, and thecatch
block will not be executed. The exception will continue to propagate up the call stack. -
Is it possible to access local variables from the `try` block inside the exception filter?
Yes, you can access local variables defined in the `try` block within the exception filter, provided they are in scope at that point.