Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Try-Catch : Logic behind the rule ?  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The catch blocks must be placed in the order specific to general - Why ?
Also, if i do it the other way round, will i get a compiler error or runtime error ?

 
Randall Fairman
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question isn't very clear (to me, anyway). Here's what I think you are asking. Suppose that there are three exception classes: EA, EB and EC, where EA extends EB, EB extends EC, and EC extends Exception. If you want to catch these things, then the catch statements must be ordered like this:



This is logical since an exception of type EA is due to a more specific problem than one of type EB, and those of type EC are the least specific -- at least they should be if the programmer defined those classes well. If you have the exceptions of type EC first, then the other two types will never be caught. In effect, they were already caught by the EC catch. Make up a few examples and feed them to the compiler.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think a bout a coin sorter. It basically has holes in it that only let coins of a certain diameter or smaller fall through. If you put the biggest (least specific) hole first, everything will fall through that one, and you'll get nothing terribly useful.

However, if you put the smallest one first, it will ONLY catch those coins. Then you have the second smallest hole, etc.

It is certainly not WRONG to put the holes in a different order. Your machine would still work, but it simply may not be terribly effective, or give you all the info you want.

I don't believe you will get any warning or error if you put them in the 'wrong' order, you will simply never get all the data you could.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I don't believe you will get any warning or error if you put them in the 'wrong' order, you will simply never get all the data you could.


You'll get a compiler error if you have a catch block that can't possibly catch anything because of the sequence. E.g.:
Compilation fails on line 7 with the message exception java.lang.NullPointerException has already been caught
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
You'll get a compiler error if you have a catch block that can't possibly catch anything because of the sequence.

Good to know.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!