aspose file tools*
The moose likes Java in General and the fly likes Rethrowing an exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Rethrowing an exception" Watch "Rethrowing an exception" New topic
Author

Rethrowing an exception

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212

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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Feb 28, 2009
Posts: 212
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Alternatively you can throw a new XYZException with the causing Exception as its cause. This is exception chaining. More about it the Java™ Tutorials.
 
Don't get me started about those stupid light bulbs.
 
subject: Rethrowing an exception