This question already has an answer here:
- Catch multiple exceptions at once? 27 answers
Using C#, is there a better way to handle multiple types of exceptions rather than a bunch of ugly catch blocks?
What is considered best practice for this type of situation?
For example:
try
{
// Many types of exceptions can be thrown
}
catch (CustomException ce)
{
...
}
catch (AnotherCustomException ace)
{
...
}
catch (Exception ex)
{
...
}
About the only other thing you can do is emulate VB.NET's exception filters:
Sometimes this is better, sometimes not. I'd mainly use it if there was some common logic I wanted in the handler, but the exceptions don't share a base type.
Unfortunately, C# does not have user exception filters like VB.NET, so you're limited to: