This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Rule Roundup Answer: catch block Exception Order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Rule Roundup Answer: catch block Exception Order" Watch "Doubt in Rule Roundup Answer: catch block Exception Order" New topic
Author

Doubt in Rule Roundup Answer: catch block Exception Order

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160

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?


SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCMJEA in progress...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

No it should be false. Why do you think it may be true ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
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

Joined: Nov 05, 2007
Posts: 661
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


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
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.
 
jQuery in Action, 2nd edition
 
subject: Doubt in Rule Roundup Answer: catch block Exception Order
 
Similar Threads
catch{} blocks
true or false?
Marcus exam queries
try {} catch() {} finally {}
try-catch statement