• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do exceptions need to be in proper order, that is catch most specific exception first ?  RSS feed

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the code -



When I remove the IOException, I still get the same output -



So, why do exceptions need to be caught in a particular order ? Imagine that the compiler allowed us to write exceptions in any order. What difference would it make ?
I don't see any difference. Then, why enforce an order ?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is already an answer in your question. If the compiler allowed us to catch exceptions in any order, it would simply catch the first one that is declared. The hierarchy is there to catch the most specific exceptions and if there is no specific exception to be caught, go for the next one which is less specific. From my point of view, it makes sens to have such a hierarchy and have the compiler checks this for us during compile time.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I remove the IOException, I still get the same output -

You get the same output because both of your catch statements do exactly the same thing and the reason you can remove the catch IOException has been explained by Joe.
If you put different print statements in the catch clauses you would see different catch clauses are being executed in your 2 scenarios. If you could swap the order and get it to compile (which you can't) then the same catch statement would execute in both of your scenarios and this would prevent you from being able to put specific handling code in place for a specific Exception.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't catch Exception. It is too generalised. Better to catch the IOException. Even that is very generalised, but that is what the method declares it throws, so that is what you have to handle.

Also, there appears to be a security risk if you do it. I had the good fortune to win this book last year. The authors mention an earlier book of theirs where they recommend not to catch NullPointerException or any of its superclasses. You would have to get the book and look to see why there is that hazard: I don't know myself. Exception is one of those superclasses.
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine that the compiler allowed us to write exceptions in any order. What difference would it make ?




If you look carefully the second catch will never be executed because the first catch is already taking care of all possibles exceptions (including Arithmetic). It doesn't make sense to have a catch that catches an already caught Exception
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would certainly be possible for the JVM to go looking for the most specific exception in the list of catch blocks, but that is making it do more work than it needs to and complicates things.

The writers of the Java Language Specification clearly though it was better to have Exceptions written in the order of specificity in the first place.
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at all the answers, this is what I have gathered so far -

If you place the exception Exception at the beginning, then you get a generic response to all exceptions. Practically speaking, that does not much sense. Its likely that you'd want to have one response to an SQLException
and another for IOException.

You could still catch it all with Exception only and then check the type of exception using instanceof and if-else. But that is messy ???
If the compiler lets you place exceptions in any order you like, then the JVM will have to look for the part of code which handles the actual exception
thrown. With ordered exceptions, there is no need for the JVM to search. If it finds the throw exception does not match the first, it proceeds to the
next caught until it finds the right handler. Without this order, you are making the JVM do more work.

Did I understand this correctly ?

 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, technically, the compiler can let you catch exceptions in any order. It knows the type hierarchy of the exceptions. It can figure out the most specific exception, and internally rearrange the compile code to put the catch clauses in the correct order. You just need to make the compiler a little more sophisticated.

The only reason that you have to catch exceptions in the right order because someone at Sun/Oracle wants you to. Java has lot of small things like this. It's just difficult to create a compiler like this. Whoever was writing the compiler didn't think it was worth it, and the rule has stuck ever since.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!