• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need clarity about Exception Handling  RSS feed

 
Rajasekhar Karanam
Greenhorn
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

In Kathy and Bates book, it is given that "Once the control jumps to catch block from the try block, the balance part of the try block is never executed."

After a few paras, it is given that, "One of the benefits of using exception handling is that code to handle any particular exception that may occur in the governed region(try block) needs to be written only once. There may be three different places in our try block that can generate a particular exception, but wherever it occurs it will be handled by the same catch block."

My question is, if the control never returns from catch block, how can it handle the same exception in the remaining part of the try block?
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't need to, because that piece of the try block will never be reached.

If you have three places in the block that can throw an exception, then if the first one actually throws it, the rest of the try block will not be executed.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajasekhar Karanam wrote:My question is, if the control never returns from catch block, how can it handle the same exception in the remaining part of the try block?


It doesn't mean that multiple exceptions will be handled in a given single pass through the try/catch. It just means that there could be multiple lines that could potentially throw an exception, and regardless of which one it is, the same catch block can handle it.



Any one of those 4 actions might throw an IOException, and if it does, regardless of which one it is, the same catch block handles that exception. Now, once one of them throws an exception, the others never execute, as stated.

So maybe 99% of the time we get through with no exceptions. Then one time the 3rd line throws IOException, so that catch block handles it, and maybe another time the first line throws IOException, and again, the same catch block handles it.
 
vive kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some rules for handling the Exception...

1. At a time only one Exception occur and at a time only one catch block executed.
2 If you uses multiple catch block then it must be ordered for example - catch for ArithmeticException comes before catch for Exception.

Example




thanks
java tutorial
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a good example.

1. We should not catch unchecked exceptions like ArithmeticException and ArrayIndexOutOfBoundsException.

2. Simply printing out the exception does not count as handling it.

3. If we are going to print the exception, we should call printStackTrace(), so we can see the code path that led to the exception.


Also, I don't recommend that tutorial you linked. It's not well written and doesn't explain things very well. I also found at least one significant error in just a few moments of looking. (It stated that the primary purpose of inheritance is code reuse, but that is wrong. The primary purpose is type specialization. If all we want is code reuse we should use composition and delegation.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!