This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B 1.5 p365 exceptions doubt

 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic