C#处理特定的异常类型

示例

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块。