Meaningless Drivel is fun!
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Rethrowing an exception" Watch "Rethrowing an exception" New topic

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

Joined: Mar 09, 2009
Posts: 600

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.

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46356
Alternatively you can throw a new XYZException with the causing Exception as its cause. This is exception chaining. More about it the Java™ Tutorials.
I agree. Here's the link:
subject: Rethrowing an exception
It's not a secret anymore!