Java Keyword: throw

class Person {

    int age;

    void setAge(int age) {
        if (age < 0)
            throwThrows an exception new IllegalArgumentException("Age must be positive");



        this.age = age;
    }
}

The throw keyword (not to be confused with the throws keyword) is used to throw an exception. After throw follows an expression of type Throwable.

Comments

Be the first to comment!