Stop 07 · Exception Handling & File I/OTopics · 4/8

throw vs throws

Part of the Java Roadmap

In short

Two words, one letter apart, with very different jobs. throw raises a problem right now. throws warns callers that a problem might come their way.

throw actually raises an exception object inside a method, while throws in a method signature declares which checked exceptions the method may pass to its caller.

The jaadu yanks down a red alarm lever on a factory wall while a big yellow hazard sign hangs on the gate behind it.

Think of it like this

The warning sign and the alarm

A sign outside a factory gate in Pune says "Danger: chemicals may leak". That sign is throws: a warning to anyone who enters. The day a pipe actually bursts, someone pulls the alarm. That is throw: the real event happening right now.

Watch it happen

An exception travelling back up the call chain

main()
pay()
Exception

pay(5000)

1/3main asks the wallet to pay ₹5,000. pay() declares throws InsufficientFundsException.

The big ideas

What you need to know

throw is a statement

throw new SomeException("message"); runs inside a method body and immediately stops that method.

throws is in the signature

void pay(long amount) throws InsufficientFundsException tells callers which checked exceptions might come out.

throws can list many

A method can declare several: throws IOException, SQLException. throw always raises exactly one object.

Passing the problem up

If a method can't sensibly handle a checked exception, declaring throws lets its caller decide what to do.

See it in code

Walk through the code

java
1class InsufficientFundsException extends Exception {2    InsufficientFundsException(long shortBy) {3        super("Short by ₹" + shortBy);4    }5}6 7public class Wallet {8    private long balance;9 10    public Wallet(long openingBalance) {11        this.balance = openingBalance;12    }13 14    public void pay(long amount) throws InsufficientFundsException {15        if (amount <= 0) {16            throw new IllegalArgumentException("Amount must be positive");17        }18        if (amount > balance) {19            throw new InsufficientFundsException(amount - balance);20        }21        balance -= amount;22    }23 24    public static void main(String[] args) {25        Wallet wallet = new Wallet(3200);26        try {27            wallet.pay(5000);28        } catch (InsufficientFundsException e) {29            System.out.println("Payment failed: " + e.getMessage());30        }31    }32}

1/4throws in the signature: the warning sign. Because InsufficientFundsException is checked, it must be declared.

A UPI-style wallet. Paying ₹5,000 from a ₹3,200 balance prints "Payment failed: Short by ₹1800".

Save yourself the pain

Common mistakes

Oops

throw InsufficientFundsException; // a class name, not an object

Do this instead

throw new InsufficientFundsException(1800); throw needs an exception object created with new.

Oops

Adding throws Exception to every method to silence the compiler.

Do this instead

Declare the specific exceptions. throws Exception tells callers nothing and forces them to catch everything.

Your mission

ATM rules

~20 min

Quick check

Which line is correct Java?

Go deeper

  1. 1Throw IllegalArgumentException for invalid method input.
  2. 2Declare throws IOException on a method and handle it in the caller.
  3. 3Explain the difference between the two in one sentence each.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge