• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rethrowing an exception

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After an exception is caught, it can be rethrown if is appropriate.

But can anybody please tell why should an exception should be rethrown.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PavanPK:

Rethrowing an exception allows you to go from a more specific exception to a more general one. As an example, let's say you have some tests and a test manager. When a test fails, you have the test manager throw a TestFailedException, to signal that a test failed. However, some of the individual tests throw their own exceptions when they fail (say, FileNotFoundException). When you run those tests, you want to get all of the failure information. By rethrowing the exception, all of the exception information is retained in the stack trace, and you can go back and diagnose the problem.

John.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49803
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively you can throw a new XYZException with the causing Exception as its cause. This is exception chaining. More about it the Java™ Tutorials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic