Throw and Throws in Java

In this article, we will learn about throw and throws in Java for exception handling. This article will focus on handling exceptions using throw and throws in Java.

What is an Exception?

An exception is an undesired event, which occurs during the execution of a program. Exceptions are basically errors that are thrown when there is some problem in our code. For example, when a number is divided by zero ArithmeticException is thrown. In java, we can throw an exception on our own.

But why would anyone want to throw an exception on their own?

Imagine a user is asked to enter their age as input. We know that the age of a person can only be a positive number. So, if the user enters a negative or zero value as their age, we may throw an exception.

There can be more cases where the input entered by the user is considered invalid for our program. In those cases, we can manually throw an exception.

What is Exception Handling in Java?

Exception Handling in Java is one of the effective ways to handle errors that occur at run time so that the regular flow of the program can be preserved. Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, ArrayIndexOutOfBoundsException, SQLException, etc.

What are Throw and Throws in Java?

‘Throw’ and ‘throws’ would seem similar having only a difference in tenses. However, in Java, these two are very distinct from each other and are used to do various tasks. Throw and Throws in java are simple keywords used in exception handling.

What is throw in Java?

The throw keyword in java is used to throw an exception manually and logically defined by the programmer during the execution of a program, the control moves from one block to another with the assumption that the exception of the error is defined and handled accordingly.

Syntax of Throw in Java is shown below:

throw throwableObject;

A throwable object can be an instance or subclass of the Throwable class. All exceptions defined in Java are subclasses of Throwable.

Implementation of Throw in Java:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    void Votingage(int age){ 
            if(age<18) 
                throw new ArithmeticException("you are not eligible to vote"); 
            else 
                System.out.println("Eligible for voting"); 
    } 
   
    public static void main (String[] args)
    {
        // Implementation starts from here
        Ideone obj = new Ideone();
        obj.Votingage(16);
        System.out.println("Program Ends");
    }
}

What is throws in Java?

Throws in Java is very similar to try-catch block in working. Throws in Java are used to declare and call an exception block.

Syntax of Throws in Java is shown below:

Type method (arguments) throws Exception1, Exception2, … { }

As seen above, a method can throw multiple exceptions, which a comma should separate in the declaration. All exceptions that can be thrown by a method should be declared in the method signature using the throws keyword.

Implementing Throws in Java:

import java.util.*;
import java.lang.*;
import java.io.*;

class TestThrows {  
    //defining division method  
    public static int divideNum(int p, int q) throws ArithmeticException {  
        int result = p / q;  
        return result;  
    }  
    //main method  
    public static void main(String[] args) {  
        TestThrows obj = new TestThrows();  
        try {  
            System.out.println(obj.divideNum(25, 0));  
        }  
        catch (ArithmeticException e){  
            System.out.println("\nNumber cannot be divided by 0");  
        }  
    }  
}

Key Differences between Throw and Throws in Java

                    Throw                   Throws
It uses the exception object. It uses the name of exception classes.
It can throw only one exception. It can throw multiple exceptions separated by a comma.
The syntax of throw is followed by an instance variable the syntax of throws is followed by the exception class names.
Throw is used inside the method Throws is used with the method signature.
It is used to simply throw an exception It is used for declaration of exception.
The throw could be utilized inside the method Throws could only be used in the method declaration.

Leave a Reply

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