wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B 1.5 p365 exceptions doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B 1.5 p365 exceptions doubt" Watch "K&B 1.5 p365 exceptions doubt" New topic
Author

K&B 1.5 p365 exceptions doubt

Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Hi folks,

This is from K&B 1.5 p365
~~~~~~~~~~~~
Rethrowing the Same Exception

Just as you can throw a new exception from a catch clause, you can also throw the
same exception you just caught. Here's a catch clause that does this:

catch(IOException e) {
// Do things, then if you decide you can't handle it�
throw e;
}

All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack).
~~~~~~~~~~~~

What exactly does it mean by All other catch clauses associated with the same try are ignored and how can I verify it?

Even when there are different catch caluses associated with a try block, they will be for handling different exceptions. I tried to verify the above by writing the following code, but I think it is not really relevant.



What I thought is that by throwing the same exception TestException1 in the catch (TestException1 e) block, I can simulate the condition. But when I say throw new TestException1() in method method1, anyway it is going to ignore other catch clauses in method callMe.

I am really stuck on how to write the correct code to verify the sentence from K&B.

Could somebody pls explain this and give relevant example if possible?
[ February 13, 2007: Message edited by: Kiran Gavate ]

Kiran
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
Your code isn't bad. Just modify "callMe()" a bit (see below):

Edit: Aargh... code tag corrected
[ February 13, 2007: Message edited by: Anton Uwe ]
Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Thanks very much for taking time to go through my code an answer the query.

My point is when you throw TestException2 in method1, it jumps to catch (TestException2 e) line in method callMe. So even if the throw e; line is not there in this catch block, it is anyway going to ignore the other two catch blocks, isn't it?

So why the book says that all other catch caluses associated with the same try are ignored when you throw the same exception in a catch block?
 
 
subject: K&B 1.5 p365 exceptions doubt
 
Similar Threads
Finally abrupt return
please explain the output of the code below
finally { } clause in Exceptions
exceptions
exception declaration