This question already has an answer here:
I am new to Java and have just came across a tutorial which uses the"Throws" keyword in a method. I have done a little research into this but still do not really understand it.
From what I have seen so far, it is telling the compiler that a certain exception may be thrown in that particular method. Why do we need to tell the compiler this? I have made many programs using merely a try-catch statement in my methods and it has worked just fine - surely it is these try-catch statements which manage exceptions, right?
Java programs throw exception whenever it is occured.However,there will be times when you want to manually throw exception,for this
throws
keyword is used.For example:
class Abc { public static void main(String args[]) throws IOException{ }}
You can manage an exception within a method using
try
andcatch
as you say. In that case, you do not need to usethrows
. For example:But suppose you had a thousand methods, all of which might throw a
SpaceInvadersException
. Then you would end up having to write all the complicated error handling code a thousand times. Of course, you could always create anErrorHandler
class with adealWithSpaceInvadersException()
method that you could call from them, but you'd still be stuck with a thousandtry
-catch
blocks.Instead, you tell the compiler that each of these thousand methods could throw a
SpaceInvadersException
. Then any method that calls one of these methods needs to deal with the error itself, either by using atry
-catch
, or by telling the compiler that it might throw aSpaceInvadersException
. This is done using thethrows
keyword, like this:In that case, you need to inform the compiler that
myMethod
could throw aSpaceInvadersException
. This means that you can't call that method without dealing with the exception in some way (try
-catch
or using thethrows
keyword on the calling method). Ifthrows
weren't there, you could call the method without doing any exception handling, and get an exception that wasn't dealt with anywhere in the program (which would be bad).Since it is always better to avoid code duplication, it is normally better to palm off your error handling to a
try
-catch
in a much higher level function than it is to deal with it separately in all of the low level methods. That is why this mechanism exists.The
throws
keyword declares that the exception can be thrown out of the method.You can also use a
catch
block to catch an exception inside the method. If you catch it and don't rethrow it, then it's not thrown out of the method.A
throws
declaration allows compile-time verification that a method either:Higher up yeah, some method will have to catch the exception that gets thrown. Just like you said, it is the try-catch blocks that manage them.
There is one exception though, and that is the RuntimeException, for which you do not need to declare the throws exception part. RuntimeException(and all of its subclasses) are called unchecked exceptions, after which you usually can not recover.
Throws is a mechanism to throw the exception to the calling method. This is generally used to throw the exception to a level where it can be handled.
A practical example is a gui based application with some backend logic. If there is a problem in the backend, you may want to show a proper message to the user. So from your backend you can throw the exception upto your UI classes and then can display a message accordingly.