DEV Community

Harini
Harini

Posted on

throw and throws in Java

Exception handling is an important feature in Java that helps developers manage runtime errors gracefully.

Two commonly used keywords in exception handling are:

  1. throw
  2. throws

Although they look similar, they serve different purposes.


What is throw in Java?

The throw keyword is used to explicitly create and throw an exception from a method or block of code.

Syntax

throw new ExceptionType("Error Message");

Example

public class ThrowExample {

public static void main(String[] args) {

    int marks = -10;

    if (marks < 0) {
        throw new ArithmeticException("Marks cannot be negative");
    }

    System.out.println("Valid Marks");
}
Enter fullscreen mode Exit fullscreen mode

}

Output

Exception in thread "main"
java.lang.ArithmeticException: Marks cannot be negative

Key Points of throw

  • Used to explicitly throw an exception.

  • Followed by an exception object.

  • Throws only one exception at a time.

  • Can throw both checked and unchecked exceptions.

  • Used inside method body.


What is throws in Java?

The throws keyword is used in a method declaration to indicate that the method may throw one or more exceptions.

It informs the caller of the method that exception handling is required.

Syntax

returnType methodName() throws ExceptionType {
// code
}

Example

import java.io.IOException;

public class ThrowsExample {

static void readFile() throws IOException {
    throw new IOException("File not found");
}

public static void main(String[] args) {

    try {
        readFile();
    }
    catch(IOException e) {
        System.out.println(e.getMessage());
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Output

File not found

Key Points of throws

  • Used in method declaration.

  • Declares possible exceptions.

  • Can declare multiple exceptions.

  • Mainly used for checked exceptions.

  • Responsibility is passed to the caller.


Example Using Both throw and throws

import java.io.IOException;

public class Demo {

static void validateAge(int age) throws IOException {

    if(age < 18) {
        throw new IOException("Not eligible to vote");
    }

    System.out.println("Eligible to vote");
}

public static void main(String[] args) {

    try {
        validateAge(15);
    }
    catch(IOException e) {
        System.out.println(e.getMessage());
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Output

Not eligible to vote

Explanation

  • throws IOException declares that the method may throw an IOException.
  • throw new IOException() actually creates and throws the exception.
  • The caller handles it using try-catch.

Difference Between throw and throws

Feature throw throws
Purpose Explicitly throws an exception Declares exceptions
Location Inside method body Method declaration
Followed By Exception object Exception class name
Number of Exceptions One at a time Multiple exceptions
Usage To generate exception To delegate exception handling
Keyword Type Statement Clause

Top comments (0)