Javathrow & throws

throw & throws

throw and throws look similar but do very different jobs. throw is a statement that actually raises an exception, right now, at a specific point in your code. throws is a clause in a method signature that declares, at compile time, that the method might raise a checked exception — it does not throw anything by itself.
throw: Raising an Exception
You use throw followed by an exception object to immediately stop normal execution and start propagating that exception up the call stack.

throw in action

Java
public class ThrowDemo {
    public static void main(String[] args) {
        checkAge(-5);
    }

    static void checkAge(int age) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative: " + age);
        }
        System.out.println("Age is valid: " + age);
    }
}
Exception in thread "main" java.lang.IllegalArgumentException: Age cannot be negative: -5
    at ThrowDemo.checkAge(ThrowDemo.java:8)
    at ThrowDemo.main(ThrowDemo.java:3)
throws: Declaring a Possibility
When a method's body can throw a checked exception (either directly with throw, or by calling another method that declares one), the compiler requires that method to declare it with throws in its signature — unless the method catches it internally instead.

throws in a method signature

Java
import java.io.IOException;

public class ThrowsDemo {
    public static void main(String[] args) throws IOException {
        readConfig();
    }

    static void readConfig() throws IOException {
        throw new IOException("config.properties not found");
    }
}
Here, readConfig declares throws IOException, which means every caller must either catch the IOException or declare it themselves — main chooses to declare it too, passing the responsibility further up to the JVM.
throw vs throws

Aspect

throw

throws

What it is

A statement

Part of a method signature

When it runs

Executes immediately at runtime

Never executes — it's a compile-time declaration

Where it appears

Inside a method body

After the parameter list, before the method body

Followed by

A single exception object

One or more exception class names

Example

throw new IOException("...");

void save() throws IOException { ... }

Worked Example: Validating Input
A very common pattern is validating a method's arguments at the top of the method and throwing IllegalArgumentException — an unchecked exception, so no throws declaration is needed — for invalid input.

Validating input with throw

Java
public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        if (initialBalance < 0) {
            throw new IllegalArgumentException(
                "Initial balance cannot be negative: " + initialBalance);
        }
        this.balance = initialBalance;
    }

    public void deposit(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException(
                "Deposit amount must be positive: " + amount);
        }
        balance += amount;
    }

    public static void main(String[] args) {
        BankAccount account = new BankAccount(100.0);
        account.deposit(50.0);
        System.out.println("Balance: " + account.balance);
        account.deposit(-10.0); // throws IllegalArgumentException
    }
}
Balance: 150.0
Exception in thread "main" java.lang.IllegalArgumentException: Deposit amount must be positive: -10.0
    at BankAccount.deposit(BankAccount.java:12)
    at BankAccount.main(BankAccount.java:19)
Tip
Because IllegalArgumentException is unchecked, no throws clause was needed on deposit. If this validation instead threw a checked exception, both deposit and main would need to declare or catch it.