Throw and Throws Keywords in Java

In java programming, If the exception occurs within the try block it is thrown.

Throw and Throws Keywords in Java is very important to throw exceptions.

a throw keyword is used to throw an exception

A system-generated exception is automatically thrown by the java run time system but if we want to manually throw an exception, we can do so by using the throw keyword. 

Exception Handling try catch finally blocks in Java

Syntax of throw

           throw    ThrowableInstance;

here, ThrowableInstance must be an object of type Throwable or subclass of Throwable.

Example of throw keyword

class DemoThrow {
    void demo() {
        try {
            throw new NullPointerException("demo"); //demo is a name of exception
        } catch (NullPointerException e) {
            System.out.println("Inside demo's catch");
            throw e; // re-throw the exception
        }
    }
}
public class TestDemo {
    public static void main(String args[]) {
        DemoThrow ob = new DemoThrow();
        try {
            ob.demo();
        } catch (NullPointerException e) {
            System.out.println("Recaught: " + e);
        }
    }
}
Inside demo's catch
Recaught: java.lang.NullPointerException: demo

In this program new is used to create an instance of NullPointerException and we get two chances to deal with the same error.

In the line  “throw new NullPointerException(“demo”);” 

throw keyword is used to throw  NullPointerException(“demo”). Here we set “demo” is a name of exception.

This NullPointerException(“demo”) catch by catch() method which resides in a demo() method.

 Line “throw e;”  re-throw the exception which is catch by catch() method which resides in a main() method.

Throws Keyword in Java

In Java programming, the throws keyword list the type of exception that a method might throw.

throws keyword inform that method throws mentioned exceptions

In the throws clause, we declare all the exceptions that a method can throw.

  Syntax of throws:         

type  method_name(parameter list) throws exception1, exception2 ….{		
}

                                               

Here, exception1, and exception2 are commas, a list of the exception that a method can throw.

Example of throws

class ExceptionClass {
    void demo() throws NullPointerException { // define throws
        System.out.println("Inside demo method.");
        throw new NullPointerException("demo");
    }
}
public class ExceptionMain {
    public static void main(String args[]) {
        ExceptionClass ob = new ExceptionClass();
        try {
            ob.demo(); // demo() definition comes within try block
        } catch (NullPointerException e) {
            System.out.println("Caught " + e);
        }
    }
}
Inside demo method.
Caught java.lang.NullPointerException: demo

Explaination: We throws NullPointerException in the line “throw new NullPointerException(“demo”);” .

Here we set “demo” as the name of the thread.

This line “throw new NullPointerException(“demo”);” throw the exception which is handled by catch block associated with try within main().

Difference between throw and throws in Java (throw vs throws)

The throw keyword is used to throw an exception from the method or from a block

the throw is used inside the method

throw new NullPointerException("demo");

throws keyword is used to inform that a method throws an exception(s)

throws keyword is used in the method header

public Scanner​(File source) throws FileNotFoundException

What is the purpose of the throw statement?

The throw is used to throw an exception from a method or from a block of code

what is the difference between throwing an exception and catching an exception?

Throwing an exception can use to throw an exception using the throw keyword.

this can be implicit or explicit

45/0 <- throw ArithmeticException 

throw ArithmeticException(“Denominator can not be zero”);

To catch an exception one or more catch blocks can be used so that program executes properly.

Create your own Exception: User-Defined exception in Java

The ______ keyword is used to manually throw an exception in java

The Throw keyword is used to manually throw an exception.

We covered Throw and Throws Keywords in Java go through following links for more.

Read More

  1. Exception Handling in Java: Hierarchy Example and Types
  2. Exception Handling in Java with Examples
  3. Exception Handling try catch finally blocks in Java

Reference

How to Throw Exceptions