throw and throws in Java

Last Updated : 13 Mar, 2026

Exception handling in Java helps manage runtime errors and prevents the program from crashing, allowing the normal flow of the application to continue. Java provides specific keywords to handle and manage exceptions effectively.

  • The throw keyword is used to explicitly throw an exception in a program.
  • The throws keyword is used in a method declaration to indicate that the method may throw certain

throw Keyword

The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions. 

Syntax:

throw Instance

Where instance is an object of type Throwable (or its subclasses, such as Exception).

Example:

throw new ArithmeticException("/ by zero");

But this exception i.e., Instance must be of type Throwable or a subclass of Throwable

When a throw statement is executed, the program flow immediately stops and the nearest try block is checked for a matching catch block.

  • If a matching catch block is found, control is transferred to that block.
  • If no match is found, the default exception handler terminates the program.

Example: This example demonstrates where an exception is thrown, caught and rethrown inside a method.

Java
class Geeks {
    static void fun()
    {
        try {
            throw new NullPointerException("demo");
        }
        catch (NullPointerException e) {
            System.out.println("Caught inside fun().");
            throw e;     // rethrowing the exception
        }
    }

    public static void main(String args[])
    {
        try {
            fun();
        }
        catch (NullPointerException e) {
            System.out.println("Caught in main.");
        }
    }
}

Output
Caught inside fun().
Caught in main.

Explanation: The above example demonstrates the use of the throw keyword to explicitly throw a NullPointerException. The exception is caught inside the fun() method and rethrown, where it is then caught in the main() method.

Example: Throwing an arithmetic exception

Java
class Geeks {
    public static void main(String[] args){
        int numerator = 1;
        int denominator = 0;

        if (denominator == 0) {
            // Manually throw an ArithmeticException
            throw new ArithmeticException("Cannot divide by zero");
        } else {
            System.out.println(numerator / denominator);
        }
    }
}

Output:

Hangup (SIGHUP)
Exception in thread "main" java.lang.ArithmeticException: Cannot divide by zero
	at Geeks.main(Geeks.java:9)

Explanation: The above example demonstrates an exception using throw, where an ArithmeticException is explicitly thrown due to division by zero.

Java throws

throws is a keyword in Java that is used in the signature of a method to indicate that this method might throw one of the listed type exceptions. The caller to these methods has to handle the exception using a try-catch block. 

Syntax:

type method_name(parameters) throws exception_list

where, exception_list is a comma separated list of all the exceptions which a method might throw.

If a method can throw a checked exception, the compiler requires it to be either handled using a try-catch block or declared using the throws keyword; otherwise, a compile-time error occurs.

  • The exception can be handled using a try-catch block.
  • The throws keyword can be used to declare the exception and delegate the handling responsibility to the caller (method or JVM).

Example 1: Unhandled Exception

Java
class Geeks {
    public static void main(String[] args)
    {
        Thread.sleep(10000);
        System.out.println("Hello Geeks");
    }
}

Output:

error: unreported exception InterruptedException; must be caught or declared to be thrown

Explanation: In the above program, we are getting compile time error because there is a chance of exception if the main thread is going to sleep, other threads get the chance to execute the main() method which will cause InterruptedException. 

Example 2: Using throws to Handle Exception

Java
class Geeks {
    public static void main(String[] args)
        throws InterruptedException
    {
        Thread.sleep(10000);
        System.out.println("Hello Geeks");
    }
}

Output:

Hello Geeks

Explanation: In the above program, by using the throws keyword we handled the InterruptedException and we will get the output as Hello Geeks.

Example 3: Throwing an Exception with throws

Java
class Geeks {

    static void fun() throws IllegalAccessException
    {
        System.out.println("Inside fun(). ");
        throw new IllegalAccessException("demo");
    }

    public static void main(String args[])
    {
        try {
            fun();
        }
        catch (IllegalAccessException e) {
            System.out.println("Caught in main.");
        }
    }
}

Output
Inside fun(). 
Caught in main.

Explanation: The above example throwing a IllegalAccessException from a method and handling it in the main method using a try-catch block.

throw Vs throws

The main differences between throw and throws in Java are as follows:

throw

throws

It is used to explicitly throw an exception.

It is used to declare that a method might throw one or more exceptions.

It is used inside a method or a block of code.

It is used in the method signature.

It can throw both checked and unchecked exceptions.

It is mainly used for checked exceptions. Unchecked exceptions can also be declared using throws, but it is not mandatory.

The method or block throws the exception.

The method's caller is responsible for handling the exception.

Stops the current flow of execution immediately.

It forces the caller to handle the declared exceptions.

throw new ArithmeticException("Error");

public void myMethod() throws IOException {}

Comment