This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Rethrowing an exception  RSS feed

 
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
Marshal
Posts: 55729
163
  • 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
Boost this thread!