What is the difference between throw and throws in Java?

Pointthrowthrows
KindStatement inside a methodKeyword in the method declaration
UseCreate and send an exception objectWarn that the method can throw exceptions
Followed byOne exception objectOne or many exception types
import java.io.IOException;

public class Main {
    static void check(int age) throws IOException {                // throws: declaration
        if (age < 0) {
            throw new IOException("Age can not be negative");       // throw: action
        }
        System.out.println("Age ok");
    }

    public static void main(String[] args) {
        try {
            check(10);
            check(-1);
        } catch (IOException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Output

Age ok
Caught: Age can not be negative

See Java Exceptions.


Learn more about the similar topics:
Tutorials
Java Exceptions: try, catch, finally and throw
Exercises & Assignments
Create a custom exception for an invalid age
Interview Questions & Answers
What is an exception in Java? What are checked and unchecked exceptions?
What is the difference between final, finally and finalize in Java?
What is the difference between throw and throws in Java?
What are the best practices for exception handling in Java?