The throw Keyword in Java


Use the throw keyword to manually throw an exception.

Source Code

public void checkAge(int age) {
    if (age < 18) {
        throw new ArithmeticException("Not Eligible for voting");
    } else {
        System.out.println("Eligible for voting");
    }
}

Throwing exceptions manually allows you to handle error conditions and invalid states in your program logically.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments