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
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Politician { public void bringToJustice() throws PoliticianAbscondingException { throw new PoliticianAbscondingException(); } } .... public class PoliticianAbscondingException extends Exception { ... } |
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Politician { public void bringToJustice() throws PoliticianAbscondingException { throw new PoliticianAbscondingException(); } } // first way of handling public void clientMethod1() { Politician aPolitician = new Politician(garbage); try { aPolitician.bringToJustice(); } catch (PoliticianAbscondingException pae){ System.err.println("Can't do much about it."); } } // another way of handling // simply propagate the exception downstream // let others also suffer! public void clientMethod2() throws PoliticianAbscondingException { Politician aPolitician = new Politician(garbage); aPolitician.bringToJustice(); } |
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
1 | throw anExceptionObject; |
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.
1 2 3 4 5 6 7 8 9 10 | public void clientMethod1() { Politician aPolitician = new Politician(garbage); try { aPolitician.bringToJustice(); } catch (PoliticianAbscondingException pae){ // don't throw PoliticianAbscondingException // throw a new kind of exception throw new Exception("He He .."); } } |
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 –
throw | throws |
---|---|
Causes an exception to be thrown | Declares 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 time | Can 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 object | A 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.