Java throw
Java throw
Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either unchecked or checked exceptions using the throw keyword. However, the throw keyword is mainly used to generate custom exceptions with the help of the throws keyword.
Syntax:
throw exception;
For example:
throw new IOException(“The device is not found.”);
Java throw Keyword
Let’s try to understand the usage of the keyword throw with the help of some examples.
Throwing an Unchecked Exception
FileName: ThrowExample.java
public class ThrowExample { // A method that throws an exception public void divideByZero() { // throwing the exception intentionally throw new ArithmeticException("0 is used as a divisor in the division process."); } // main method public static void main(String argvs[]) { // creating an object of the ThrowExample class ThrowExample obj = new ThrowExample(); try { // invoking the method divideByZero obj.divideByZero(); } // handling the exception catch(Exception te) { System.out.println(te); } } }
Output:
java.lang.ArithmeticException: 0 is used as a divisor in the division process.
Explanation: The try-catch block handles the explicitly raised exception in the divideByZero() method. Note that ArithmeticException is an example of an unchecked exception.
Throwing a Checked Exception
FileName: ThrowExample1.java
// importing the exception IOException import java.io.IOException; public class ThrowExample1 { // main method public static void main(String argvs[]) { // handling the exception try { throw new IOException("Unable to find the file."); } catch (IOException IOExObj) { System.out.println(IOExObj.getMessage()); } System.out.println("Remaining code that follows the try - catch block."); } }
Output:
Unable to find the file. Remaining code that follows the try - catch block.
Explanation: Now, the throw keyword generates a checked exception that is being handled in the try-catch block. Note that the IOException is a checked exception.
Throwing a Custom Exception
The Java throw keyword can also be used to throw a custom exception. Custom exceptions play a very important role in Java projects. It is seen that most of the time, custom exceptions are used more frequently as compared to inbuilt exceptions. Custom exceptions come in very handy to show the project-specific messages. The following illustrates how one can raise a custom exception.
FileName: ThrowExample2.java
class InvalidNumberException extends Exception { // constructor of the class InvalidNumberException(String str) { // invoking the constructor of the parent class super(str); } } public class ThrowExample2 { // main method public static void main(String argvs[]) { try { // custome exception is raised throw new InvalidNumberException("25 is not valid number. Number should be less than 20."); } // handling the raised custom exception catch(Exception e) { System.out.println("An exception occurred: "+ e); } System.out.println("Code following the try catch block."); } }
Output:
An exception occurreda: InvalidNumberException: 25 is not valid number. Number should be less than 20. Code following the try catch block.
Explanation: Exception class must be used as a parent class to create our own exception. In order to show the desired messages, the constructor of the parent class, i.e., Exception class, must be invoked. The super(str); statement is doing the same. The rest of the code is straightforward to understand.