Java throw and throws Keywords


In this blog post, we will explore the purpose and usage of Java’s throw and throws keywords. Java provides two essential keywords, throw and throws, that enable programmers to manage and propagate exceptions effectively and allow developers to gracefully handle unexpected errors or exceptional situations.

1. The throw Keyword

The throw keyword is used to explicitly throw an exception in Java. It is typically followed by an instance of an exception class or an object that implements the Throwable interface. Here’s an example:

public void divide(int dividend, int divisor) {
    if (divisor == 0) {
        throw new ArithmeticException("Divisor cannot be zero.");
    }
    int result = dividend / divisor;
    System.out.println("Result: " + result);
}

In the above example, if the divisor is zero, we throw an ArithmeticException explicitly, indicating the invalid division operation. The program execution is halted, and the exception is propagated up the call stack until it is caught by an appropriate exception handler.

2. The throws Keyword

The throws keyword is used in a method declaration to specify the exceptions that might be thrown by that method. By declaring the potential exceptions, you are indicating to the caller that they need to handle or propagate those exceptions further. Let’s consider the below example:

public void readFromFile(String filename) throws FileNotFoundException {
    File file = new File(filename);
    FileInputStream fis = new FileInputStream(file);
    // ... additional code
}

In this example, the readFromFile method declares that it may throw a FileNotFoundException if the specified file is not found. This informs the calling code to handle or propagate the exception appropriately. If the exception is not caught, it will be propagated up the call stack until it reaches a suitable exception handler.

3. Combining throw and throws

Let’s create a method that validates a user’s age and throws a custom exception if the age is invalid. Here’s the implementation:

public void validateAge(int age) throws InvalidAgeException {
    if (age <= 0) {
        throw new InvalidAgeException("Age must be a positive value.");
    }
    System.out.println("Valid age: " + age);
}

In the above example, the validateAge method throws an InvalidAgeException if the age provided is less than or equal to zero. By declaring the throws clause, we notify the caller that they must handle or propagate this custom exception.

Summary

The throw and throws keywords are essential tools for handling exceptions in Java. The throw keyword allows you to explicitly throw exceptions when certain conditions are met, while the throws keyword informs the caller about the potential exceptions that might be thrown by a method.

References

  1. How to Throw Exceptions- JavaDoc
  2. File Handling Example in Java

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.