Skip to content
Veltoryx Technologies
ProLesson 5.4Intermediate3 min readUpdated 30 October 2026

throw, throws and custom exceptions

Throw your own exceptions, declare them with throws, create custom exception classes, and wrap exceptions without losing the original cause.

So far we've caught exceptions that Java threw. Your own code can throw them too, to stop on invalid data or to report a business problem clearly.

01throw vs throws

  • throw is a statement that throws an exception object now.
  • throws is part of a method signature that warns callers which checked exceptions the method may throw.
ThrowDemo.java
public class ThrowDemo {
    static double discount(double price, int percent) {
        if (percent < 0 || percent > 100) {
            throw new IllegalArgumentException("Percent must be 0-100, got " + percent);
        }
        return price * (100 - percent) / 100;
    }

    public static void main(String[] args) {
        System.out.println(discount(1000, 20));
        try {
            discount(1000, 150);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }
}
Expected output
800.0
Percent must be 0-100, got 150

Loading the rest of the lesson…