Skip to content

Catch-Clause Order

Question

Does the order of catching exceptions matter?

Short interview answer

Yes. Catch more-derived exception types before their base types. A base-type catch first makes later derived catches unreachable.

Detailed answer

For example, the derived type must appear first:

try { OpenConfiguration(); }
catch (FileNotFoundException ex) { CreateDefaultConfiguration(ex); }
catch (IOException ex) { ReportIoFailure(ex); }
catch (Exception ex) { LogUnexpectedFailure(ex); }

Putting catch (IOException) first would make the FileNotFoundException clause unreachable. Order also communicates which failures receive specific recovery behavior and which are only logged at a boundary.

Sources