In this article, we will learn about the throw keyword in Java in detail for exception Handling with a set of examples. But first, we need to understand what are exceptions in java.
What are Exceptions in Java?
In Java, there are 2 types of exceptions:
These exceptions are checked at the run-time, not at the compile-time. These exceptions include:
- ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException exceptions under Error class, etc.
These exceptions are checked at the compile time. These exceptions include:
- IOException, InterruptedException, etc.
Note: Unchecked exceptions occur due to programming errors so generally, we don't handle them instead it's a good practice to correct them.
Now let's see how to handle checked exceptions in Java in detail!
Java throw keyword
The throw keyword in Java is used to throw an exception explicitly from a method or any block of code. When an exception is thrown, the flow of program execution transfers from the try block to the catch block. An exception can be either checked or unchecked.
Additionally, the throw keyword is majorly used to throw custom exceptions. We use the throw keyword within a method.
The syntax for using the throw keyword to throw exceptions is:
Here, the exception i.e, Instance must be of type Throwable or a subclass of Throwable. For example:
A throwable object is an instance of the class Throwable or a subclass of the Throwable class. The throw statement is used together with an exception type which is often used together with a custom method.
Note: Unlike C++, data types such as int, char, floats, or non-throwable classes cannot be used as exceptions.
When the throw statement is executed, the flow of execution of the program stops immediately and the nearest enclosing try block is checked to see if it has a catch statement that matches the type of exception.
If that catch handles the same type of exception then, control is transferred to that statement otherwise next enclosing try block is checked, and so on. If no matching catch is found then the default exception handler will halt the program.
Example 01) Throwing Unchecked Exception
In this example, we will throw an unchecked exception if the age is below 18:
In this example, we are explicitly throwing an ArithmeticException.
Note: ArithmeticException is an unchecked exception. It's usually not necessary to handle unchecked exceptions.
Example 02) Throwing Unchecked Exception
In this example, we are again throwing the unchecked exception as given below:
Example 03) Throwing Unchecked Exception
Another example showing the use of the throw keyword:
Example 04) Throwing Checked Exception
In this example, we will throw a checked exception named IOException with the message we passed to its constructor :
The findFile() method throws an IOException with the message we passed to its constructor. We are specifying it in the throws clause because it is the checked exception.
The methods that call this findFile() method need to either handle this exception or specify it using the throws keyword themselves.
We have handled this exception in the main() method. The flow of program execution transfers from the try block to the catch block when an exception is thrown. So, the rest of the code in the try block is skipped and statements in the catch block are executed.
throw and throws in Java
In exception handling, we also use the throws keyword. Some of the differences between throw and throws are given below:
|Used to throw an exception for a method
|Used to indicate what exception type may be thrown by a method
|Cannot throw multiple exceptions
|Can declare multiple exceptions
In this article, we learned in great detail about the throw keyword in Java and how to throw exceptions using them. Using throw while exception handling increases the quality of our code. Some of the crucial points to keep in mind about the throw keyword are :
- used to throw custom exceptions.
- We use the throw keyword within a method.
- The instance must be of type Throwable or a subclass of Throwable.
- The throw statement is used together with an exception type which is often used together with a custom method.
Congratulations on getting this far! Now give yourself a pat on the back. Good job!