throw Keyword in Java Exception Handling

In the post try-catch block we have already seen some examples of exceptions but those all are thrown by Java run time. You can choose to throw an exception explicitly in your Java code, that can be done using throw statement in Java.

General form of throw statement in Java

The throw statement requires a single argument; a throwable object.

Here throwableObj must be an instance of Throwable class or any of its subclass.

How to get Throwable class instance

If you want to throw an exception explicitly you can get this Throwable class instance in two ways-

  1. You can create an instance using new operator.
  2. You can use the exception parameter of the catch block and rethrow the exception.

If there is a throw statement in a Java code then execution stops as soon as throw statement is encountered. Nearest catch block is checked for the matching exception type, if catch block has the matching exception type then that block handles the thrown exception. If matching exception type is not found then the next higher context is checked and so on.

Throw statement example with new exception instance

As stated above one of the way you can throw exception in your code is by creating an exception instance using new operator.

Output

In the above code exception is thrown if 2 arguments are not passed. The constructor used while creating exception instance is the one that takes String argument. That way you can pass a clear reason why exception is thrown. There is a method getMessage() method in Exception classes that can be used to display that message.

Java throw statement example when same exception is rethrown

You can also rethrow the exception that is caught in a catch block. One of the common reason to do that is see if there is any exception handler for that specific exception which is thrown. As example if you have a catch block with Exception as exception type, since Exception is higher up in the exception hierarchy it will catch most of the exceptions. You can rethrow that exception to be caught by an exception handler with the specific exception type.
Example code

Output

In the above code division method has a catch block with parameter of class Exception. From there you are rethrowing the caught exception. In the calling method it is caught again by the catch block which has parameter of type ArithmeticException.

Rethrowing a different exception

You can also catch one type of exception and rethrow exception of another type. Actually it is a best practice for exception handling to do that when you are propagating exception through separate layers. That helps in preserving loose coupling of your code.
As example – In your DB layer SQLException is caught there is no sense in letting the same exception propagate to your business layer. In this case best thing to do is catch the SQLException (checked exception) in your DAO layer and rethrow another exception (unchecked) that should propagate to business layer. You should send the original exception instance as a parameter.

That’s all for the topic throw Keyword in Java Exception Handling. If something is missing or you have something to share about the topic please write a comment.


You may also like

One Comment

  1. Pingback: Difference Between throw and throws in Java - KnpCode

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.