• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Rule Roundup Answer: catch block Exception Order

 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

true or false:

#136 In a try/catch construct, the catch blocks may be written in any order and the most narrow/specific catch argument is chosen at runtime.


Correct answer is FALSE. I have always answered the correct answer here because I could remember that multiple catch blocks must be ordered from smallest to biggest (that is, "youngest to oldest" in the Exception inheritance tree)

But looking back at my notes, I found out that "siblings of the inheritance tree can be in any order of the catch block."

Now I got confused, does this mean that the answer to this question maybe true?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it should be false. Why do you think it may be true ?
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to clear out my confusion that this statement can be TRUE for the case of �Siblings of the Inheritance Tree�.

For example, I am creating a Banking application. And I want to have a BaseException, the mother of all exceptions of my application. The BaseException extends the Java Exception class and have 2 direct subclasses, DBException and TransactionException. And TransactionException have 1 subclass called DepositException.

And a simple Deposit Transaction which involves a DB lookup and an update to the account:



Being aware that TransactionException and DBException are both "siblings" of the inheritance tree, can I interchange their catch block order:



Is this legal?

[ June 02, 2008: Message edited by: Denise Saulon ]

[ June 02, 2008: Message edited by: Denise Saulon ]
[ June 02, 2008: Message edited by: Denise Saulon ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried it out but I believe it's legal.
But the answer to your question will still be false as ,Generally , you cannot write exceptions in any order .
The parent exception should always be in the end.
But if you specify , between sibling exception then it will true.

However no such condition is mentioned .
So the answer is False
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I believe that it is also legal..

The statement becomes true when we're talking about Exceptions with the same parent class. Can we change the question into something like this:


#136 In a try/catch construct, the catch blocks are written in any order and the most narrow/specific catch argument is chosen at runtime.


So the answer will always be FALSE. Just a thought.

Or maybe it's just really about practice on deciphering questions. I haven't really tried a lot of mock exams yet so I guess I've got a long way to go.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic