Order of Catch Blocks in Java
Fast recall
Quick revision
Core answer
catch blocks from most specific to most general (subclasses before superclasses). If you put a parent type like Exception before a child like IOException, the child block becomes unreachable and the code won't compile. The JVM checks catch clauses top-down and runs the first one that matches.Whether you know catch blocks are matched top-down and that a broader type before a narrower one is a compile error
Putting catch (Exception e) first, which makes every following specific catch unreachable and fails compilation
Mention that multi-catch can replace several ordered blocks when handling is identical, and that order is irrelevant for unrelated sibling types
Interview-ready guide
Interview answer
The rule is to order your catch blocks from most specific to most general — subclasses first, superclasses last.
The reason comes down to how the JVM matches them. When an exception is thrown, the JVM scans the catch blocks top to bottom and runs the first one whose type matches. It doesn't look for the closest or best match — it just takes the first one that fits.
So if you put a broad type like Exception at the top, it would catch everything, and any more specific catch block below it, like I O exception, could never be reached. Java actually treats that as unreachable code and gives you a compile error, with a message saying the exception has already been caught. So the mistake gets caught at build time, which is nice.
The correct ordering is to put the narrowest types first — say FileNotFoundException, then the broader IOException, and then maybe a catch Exception at the very bottom as a last-resort fallback. That way every block is reachable and each handles exactly what it should. One nuance is that ordering only matters when the types are in a parent-child relationship.
If you're catching two unrelated siblings, like I O exception and S Q L exception, neither can catch the other, so the order between them doesn't matter at all. And if you find yourself writing several catch blocks with identical bodies, that's a sign to use multi-catch instead, where you list the types separated by a pipe and handle them in one block.
I'd also caution against making that trailing catch Exception too broad, because a catch-all can quietly hide bugs you'd rather see fail.
Order of Catch Blocks in Java
Direct Answer
Order multiple catch blocks from most specific to most general (subclasses before superclasses). If you put a parent type like Exception before a child like IOException, the child block becomes unreachable and the code won't compile. The JVM checks catch clauses top-down and runs the first one that matches.
Common Mistakes & Pitfalls
catch (Exception e)at the top. Every specific block after it is unreachable — compile error.- Assuming the JVM picks the 'best' match. It picks the first match in source order, not the closest type.
- Over-broad fallback. A trailing
catch (Exception)can hide bugs; keep it narrow or log clearly. - Duplicating identical handling — use multi-catch (
catch (A | B e)) instead of repeated blocks.
Check your understanding
Answer these without looking back at the article.
- Can you have catch(Exception e) as the last catch block?
- What is the difference between catch(Exception e) and catch(Throwable e)?
- When would you use catch(Throwable e)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.