Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rethrowing the same exception

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have just gone through the Exception Handling chapter and found don't understand when should I use the rethrowing of same exception?
e.g.
catch(NullPointerException e) {
e.printStackTrace();
throw e;
}
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Suppose a function A() calls another function B() and the exception is thrown in function B>now
when u say throw e( any exception) control is passed to the catch block of function A() else if u don say throw e then contol willl pass to the next command in function A() after the function B() is called
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe in some circumstances you need to catch an exception to present some error message to the user, to do some clean-up or anything else and then you still need to rethrow the same exception further to the calling method because of contract requirement or because you simply want the caller method to know what happened.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew
I closed the other thread you had with the same name because you asked basically the same question twice.
In the future you can use the edit feature ( to edit your previuos post instead of creatng a new one. Besides, most people will think it is an exact duplicate if they have the exact same name and not read them both anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic