#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...
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 ]
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!
Joined: Jan 01, 2007
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.
subject: Doubt in Rule Roundup Answer: catch block Exception Order