Exceptions in Java VII: throw and throws in Java

Java has two similar sounding exception related constructs – throw and throws. These two constructs are a constant source of confusion among beginners. Having already looked at the three of the five exception related constructs, we will now cover the other two – throw and throws in this post.

The throws Keyword

The throws keyword is used to declare a list of checked exceptions that a method is likely to throw. The sole purpose of throws statement is to inform the compiler and the programmer which exceptions are likely to occur during the execution of a method.

The general syntax of throws clause is

<MethodReturnType><AccessModifiers> <methodName> throws CheckedException1, CheckedException2 { …

Note that only methods are allowed to use throws clause.

The callers of the method have to explicitly deal with all the exceptions listed in the throws clause. The compiler checks to see if the caller has either re-thrown or handled the exceptions declared in the throws clause.

Generally,  it is considered a bad practice to have a throws clause in the public API methods. Because the throws clause forces your client to handle those exceptions, it means extra work for them. To the extent possible minimize or avoid usage of throws.

An Example of throws

To demonstrate the use of throw clause we again turn to our Politician example. Suppose, out Politician class has the following method

We also have an exception by the name PoliticianAbscondingException.  The method bringToJustice declares that it might throw this exception.

Else where we are instantiating the Politician class to use the method bringToJustice. Whenever we want to use this method we have to deal with the exception in one of the two ways demonstrated in the code snippet below –

Notice how cumbersome it is to use a method that declares exception using throws clause. And if the Politician absconds it is very unlikely that code calling this method would be able to do anything about it.

The throw Keyword

The throw keyword is used to transfer execution control flow from the current location to place where the thrown exception is handled.

The throw keyword is used to tell the runtime that you have encountered a special situation and you would handle it as a response to an exception.

The syntax of the throw statement is

The moment the throw statement is encountered the execution of the rest of the statements in the current code block is cancelled.

Continuing with our Politician‘s example above, suppose, we are too embarassed to let the world know why we could not server justice to politican. So, we don’t want others to know about PoliticianAbscondingException.  Instead, we just want to say that justice could not served.

In the above example we are catching one exception and throwing another. This is a very common (and recommended) pattern. You should let your clients know only what they need to know.

Difference Between throw and throws

This is one of the more popular interview question for Java beginners. The table below lists the differences between throw and throws –

throwthrows
Causes an exception to be thrownDeclares that an exception MIGHT be thrown
Is a statement. Definitely causes change in control flow.Is a declaration. Does not cause change in control flow.
You can throw only one exception at a timeCan declare a comma separated list of exceptions
throw can occur inside method or constructor body. Constructors CAN throw exception.throws can occur only in the declaration of methods or constructors. Constructors CAN declarre that they may throw exception.
A throw statement has exactly one Exception objectA throws statement is followed by class names of one or more Exceptions

We now turn to declaring and using our custom built exceptions in the next lesson.

Leave a comment

Your email address will not be published. Required fields are marked *