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{
----//----
}
The difference lies in the way you call the method.
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].
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:
If you think you can handle and recover from the Exception
, that you should catch
and do the needful.
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.
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.
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.
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, A
s specification must be at least as restrictive as that of the base.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With