I’ve never had the need catch more than one exception at a time before. But here’s my scenario. I want to “try” to copy a file. If the destination doesn’t exist, I don’t want to be bothered by it. But I DO still want to catch any other type of exception. A UnauthorizedAccessException for example. Below are two things I’ve tried, but I’ve seen both examples used on the web. Is one better coding than another. Or am I completly wrong on both? Thanks.
catch (DirectoryNotFoundException)
{
// Do Nothing
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
or
catch (Exception ex)
{
if (ex is DirectoryNotFoundException)
{
// Do nothing
return;
}
else
{
MessageBox.Show(ex.Message);
}
}
IMHO, Having multiple catch will reduce the code-readability. So this is what I would like to suggest.