Better way to ignore exception type: multiple catch block vs. type querying

Posted by HuBeZa on Stack Overflow See other posts from Stack Overflow or by HuBeZa
Published on 2011-01-16T08:19:13Z Indexed on 2011/01/16 8:53 UTC
Read the original article Hit count: 209

Filed under:
|
|

There are situations that we like to ignore a specific exception type (commonly ObjectDisposedException). It can be achieved with those two methods:

try
{
    // code that throws error here:
}
catch (SpecificException) { /*ignore this*/ }
catch (Exception ex)
{
    // Handle exception, write to log...
}

or

try
{
    // code that throws error here:
}
catch (Exception ex)
{
    if (ex is SpecificException) { /*ignore this*/ }
    else
    {
        // Handle exception, write to log...
    }
}

What are the pros and cons of this two methods (regarding performance, readability, etc.)?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET