This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 38075
    
  22
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: http://aspose.com/file-tools
 
subject: Rethrowing an exception
 
Similar Threads
Q on Exception Handling
Rethrowing exception?
Transaction rollback on another AppServer
exceptions
Query about MDB throwing application exception