处理特定的异常类型

try
{
    /* code to open a file */
}
catch (System.IO.FileNotFoundException)
{
    /* code to handle the file being not found */
}
catch (System.IO.UnauthorizedAccessException)
{
    /* code to handle not being allowed access to the file */
}
catch (System.IO.IOException)
{
    /* code to handle IOException or it's descendant other than the previous two */
}
catch (System.Exception)
{
    /* code to handle other errors */
}

请注意按顺序评估异常并应用继承。因此,你需要从最具体的开始,并以他们的祖先结束。在任何给定点,只会执行一个 catch 块。