Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between "throws" with the method name and catching an exception?

I am very new to exception handling in java, hence this question might seems stupid but please let it be answered. Suppose i have a method A in which some part of code can throw an exception B then what is the difference between catching the exception in the method or writting the method declaration as:-

void A() throws B{
     ----//----
     }
like image 678
amod Avatar asked Aug 23 '11 11:08

amod


4 Answers

The difference lies in the way you call the method.

  1. If your method signature says throws Exception, the code that calls your method must deal with the Exception either by catching it [via a catch clause] or throw it back [via throws clause].

  2. If you catch the Exception, you are basically trying to handle the Exception yourself and the code that calls your method does not have to deal with the Exception.

My advice is as follows:

  1. If you think you can handle and recover from the Exception, that you should catch and do the needful.

  2. If you cannot deal with the Exception properly that do not catch it, you must throw it back.

NOTE: It is a bad programming style to catch an Exception and re-throw it back [although you can do it]. If you wrap the Exception with another one, then it is ok. But in this case it must make sense to wrap the Exception with another one.

Update: The idea is, no matter what number of layers your application has, somebody has to deal with that throws Exception clause. You cannot just let a StackTrace appear to the user in the middle of his activity with the application. The point to consider is "Can you recover from the error and resume your processing?" If yes, then handle the Exception, else throw it back to the layer that can at least show a meaningful message to the user about what happened.

like image 119
Swaranga Sarma Avatar answered Nov 11 '22 03:11

Swaranga Sarma


If you catch the exception in A() you have to react to the error in your method A(), for example by trying the offending code again, logging the error, etc.

If you (re)throw the exception, you expect/forcce the method that called A() to handle the exception or it goes unhandled.

like image 36
Jacob Avatar answered Nov 11 '22 02:11

Jacob


The difference is that if you catch B in the method body, you're handling the exception. You could throw another exception, but unless you do so the problem is considered solved as far as Java is concerned. By using the throws declaration on the method signature, you're indicating that your method can throw exceptions of type B and other pieces of code using it will be forced to deal with it, or throw it further up.

like image 2
G_H Avatar answered Nov 11 '22 04:11

G_H


What you wrote is merely an exception specification, which tells everyone which exceptions you may possibly throw. That doesn't mean that you will actually end up throwing any exceptions.

On the other hand, try/catch is about exception handling: You surround code in a try block which may throw an exception and then handle the exceptions as they come in.

From inside your class A you are only allowed to throw exceptions which are listed in the exception specification; if A derives from another class or interface, As specification must be at least as restrictive as that of the base.

like image 2
Kerrek SB Avatar answered Nov 11 '22 04:11

Kerrek SB