Handling specific exception types
suggest changetry
{
/* 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 a descendant other than the previous two */
}
catch (System.Exception)
{
/* code to handle other errors */
}
Catch clauses are tested in order, and a clause matches derived types. Put the most specific types first and their ancestors later. Only one catch block runs.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents