處理特定的異常型別

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