Site icon

Difference between throw, throws & Throwable in java (with example).

1. What is throw in java?

Example: How to use throw keyword in java?

We will discuss an example of throw keyword, In the following example, the throw statement is used to throw a IllegalArgumentException , when the salary or age of person is negative (less than zero). The block then catches the exception and handles it by printing a message.

public class Person {
    private String name;
    private int age;
    private double salary;

    public Person(String name, int age, double salary) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative");
        }

        if (salary < 0) {
            throw new IllegalArgumentException("Salary cannot be negative");
        }

        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public static void main(String[] args) {
        try {
            // Creating a person with negative age to trigger an exception
            Person person1 = new Person("John", -25, 50000.0);
        } catch (IllegalArgumentException e) {
            System.out.println("Error: " + e.getMessage());
        }

        try {
            // Creating a person with negative salary to trigger an exception
            Person person2 = new Person("Alice", 30, -60000.0);
        } catch (IllegalArgumentException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Person class has a constructor that takes a name, age, and salary as parameters. Before setting the values, it checks whether the age and salary are negative.

If either of them is negative, it throws an IllegalArgumentException with an appropriate error message.  We are catching the exceptions in the main method, so that we can take appropriate corrective actions.

2. What is throws?

public static void printPage() throws IOException

Example: How to use throws in java?

import java.io.IOException;

public class DemoThrows {
    public static void printPage() throws IOException {
        throw new IOException("Simulated IOException in printPage method");
    }

    public static void main(String[] args) {
        try {
            printPage();
        } catch (IOException e) {
            System.out.println("An IOException occurred: " + e.getMessage());
        }
    }
}

The printPage method is declared to throw an IOException, and the main method calls this method within a try-catch block to handle any IOException thrown my printPage method.

3. What is Throwable in java?

The class hierarchy of Throwable is as follows:

Exception class:

Fig 1: Exception class hierarchy

Error:

Program: How to use Throwable Exceptions

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileProcessor {
    public static void processFile(String filePath) throws FileNotFoundException {
        File file = new File(filePath);

        try {
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
            throw e;
        } catch (Exception e) {
            System.out.println("An unexpected exception occurred: " + e.getMessage());
        } 
    }

    public static void main(String[] args) {
        String filePath = "nonexistentfile.txt";

        try {
            processFile(filePath);
        } catch (FileNotFoundException e) {
            System.out.println("Error: " + e.getMessage());
        } catch (Throwable t) {
            System.out.println("Unexpected error: " + t.getMessage());
        }
    }
}

Summary – throw, throws & Throwable in java

  1. throw & throws are keyword in java.
    • Throwable is super class of all exceptions ( & errors).
  2. throws signifies, the kind of exception, the method can throw
    • throw is used to throw exception, from method or executable block.
Exit mobile version