Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats the point?

 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the point of rethrowing an exception from a catch block when it cant be handled by other catch blocks. And if the exception rebounds back to the calling method, isnt a JVM shutdown then inevitable?
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the exception rebounds back to the calling method, it isn't inevitable that you're going to have a JVM shutdown - there are times when I'm programming that the "proper" response is to kick the message back a step to let the calling method deal with the matter. For instance, when I'm writing a utility class for large portions of an app, I'm going to want to handle any issues that arrise in different ways depending on what's happening at the moment when it was called, rather than having a single standard method. Perhaps in one spot it's appropriate to stop processing, while at others it's more appropriate to simply flag a warning and continue.

As for the point of rethrowing the exception, again, sometimes there's other things you're going to want to do with the thrown exception, after initially dealing with it. (At my office, for instance, we sometimes wrap our exceptions in another exception as we throw back to the calling method, to have a standard message appear for our users, while allowing us to see what was going on.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic