Java From Beginner To Advanced

0% completed

Previous
Next
Java throw Keyword

What is the throw Keyword?

The throw keyword in Java is used to throw an exception manually. Unlike exceptions that are thrown automatically by the Java Virtual Machine (JVM) when an error occurs, using throw allows developers to create and throw exceptions based on specific conditions within their code. This capability is essential for implementing custom error handling logic and enforcing application-specific rules.

When to Use the throw Keyword

  • Input Validation: Ensuring that method parameters meet certain criteria before processing.

  • Custom Exception Scenarios: Creating exceptions that represent specific error conditions unique to the application.

  • Enforcing Business Rules: Throwing exceptions when business logic rules are violated.

  • Error Propagation: Propagating exceptions to higher levels of the application for centralized handling.

Syntax of the throw Keyword

throw new ExceptionType("Error message");
  • ExceptionType: The type of exception to throw (e.g., IllegalArgumentException, CustomException).
  • Error Message: A descriptive message that provides details about the exception.

Example:

throw new IllegalArgumentException("Invalid input provided.");

Example 1: Throwing a Built-in Exception

This example demonstrates how to use the throw keyword to throw a built-in exception when a method receives an invalid argument.

Java
Java

. . . .

Explanation:

  • validateAge Method:
    • Checks if the provided age is negative.
    • If age is negative, it throws an IllegalArgumentException with a descriptive message.
  • main Method:
    • Calls validateAge with an invalid age (-5).

    • The exception is caught in the catch block, and an error message is printed instead of terminating the program abruptly.

Example 2: Throwing Multiple Exceptions

This example illustrates how a single method can throw different types of exceptions based on varying conditions using the throw keyword.

Java
Java

. . . .

Explanation:

  • processInput Method:
    • Throws an IllegalArgumentException if number is negative.
    • Throws a NullPointerException if text is null.
  • main Method:
    • Calls processInput with invalid arguments (-10 and null).
    • Catches both IllegalArgumentException and NullPointerException in a multi-catch block and prints the error message.

Note: In this run, since the first condition (number < 0) is true, the second condition (text == null) is never evaluated. If number were valid, the second exception would be thrown and caught accordingly.

The throw keyword is a powerful tool in Java's exception handling arsenal, enabling developers to create robust applications that can handle errors gracefully and maintain consistent behavior even in the face of unexpected situations. By understanding how to use throw effectively—whether throwing built-in exceptions or crafting custom ones—you can enhance the reliability and maintainability of your Java programs.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next