DevNotes
Support
Exception Handling · Chapter 08

throw vs throws

Throwing vs declaring — JVM behaviour, checked/unchecked rules, re-throwing and wrapping, throw null, constructors and overriding.

◷ 6 min read§ 16 sections? 10 interview questions{ } 29 code examples

#Introduction

One of the first exception-handling questions in almost every Java interview is:

"What is the difference between throw and throws?"

Many developers answer: "throw throws an exception and throws declares it." While correct, this answer is incomplete. To answer confidently in a senior interview, you should understand:

  • JVM behaviour
  • Exception propagation
  • Checked vs unchecked interactions
  • Inheritance rules
  • Method overriding
  • Spring Boot usage
  • Enterprise best practices

#What is throw?

throw is a keyword used to explicitly create and throw an exception object.

Java
throw exceptionObject;
Java
throw new RuntimeException("Invalid User");
Create exception object
Throw exception
Current method stops
Search for catch block
Found?
handle
propagate to caller

#What is throws?

throws is used in the method declaration to indicate that the method may throw one or more exceptions.

Java
returnType method() throws ExceptionType {
}
Java
public void readFile() throws IOException {
}

Meaning: "I may throw IOException. The caller must handle it."

#Simple Comparison

throwthrows
Used inside a methodUsed in a method declaration
Throws an exception objectDeclares possible exceptions
Followed by an objectFollowed by exception classes
One exception object at a timeMultiple exception types allowed
Transfers control immediatelyNo immediate control transfer

#Examples

throw

Java
public void validateAge(int age) {
    if (age < 18) {
        throw new IllegalArgumentException("Age must be at least 18");
    }
}
Method starts
Condition true
throw
Method terminates
Caller handles

throws

Java
public void read() throws IOException {
    FileReader reader = new FileReader("test.txt");
}
Method starts
IOException may occur
Declared using throws
Caller decides

#throw with Checked and Unchecked Exceptions

Checked exception, declared

Java
public void upload() throws IOException {
    throw new IOException("Upload Failed");
}

Compilation succeeds because the checked exception is declared.

Missing throws

Java
public void upload() {
    throw new IOException();
}
Compilation error
Unhandled exception type IOException

Reason: checked exceptions must be caught or declared using throws.

Unchecked exception

Java
public void calculate() {
    throw new ArithmeticException();
}

No throws needed, because ArithmeticException → RuntimeException → unchecked.

#Can throw Throw null?

Java
throw null;

Compilation: ✅ success. At runtime:

Output
NullPointerException

The JVM attempts to throw a Throwable reference, finds null, and throws a NullPointerException instead.

#Multiple Exceptions

Multiple exceptions with throws

Java
public void process() throws IOException, SQLException, ParseException {
}

A method may declare multiple exception types.

Can throw Throw Multiple Exceptions?

No. One throw statement throws only one exception object. If different exceptions are needed, use conditional logic:

Java
if (type.equals("FILE")) {
    throw new IOException();
} else {
    throw new SQLException();
}

#Re-throwing and Wrapping

Re-throwing Exceptions

Java
try {
    readFile();
} catch (IOException e) {
    throw e;
}

The same exception is propagated.

Wrapping Exceptions

Instead of re-throwing directly:

Java
catch (IOException e) {
    throw new RuntimeException(e);
}

Better:

Java
catch (IOException e) {
    throw new FileUploadException("Upload Failed", e);
}

This provides meaningful business context while preserving the original cause.

throw vs throws Together

Java
public void save() throws IOException {
    throw new IOException("Disk Full");
}
throws — compiler knows
throw executes
Exception created
Caller handles
throws declares; throw executes

#Constructors, Interfaces and Overriding

Constructor with throws

Constructors may also declare exceptions:

Java
public Employee() throws IOException {
}

The caller must handle or declare the exception.

Interface Methods

Java
interface Reader {
    void read() throws IOException;
}

Implementation:

Java
class FileReaderImpl implements Reader {
    @Override
    public void read() throws IOException {
    }
}

The implementing class must follow the exception contract.

Method Overriding Rules

Parent:

Java
void read() throws IOException {
}

Child:

Java
void read() throws FileNotFoundException {   // ✅ Valid — narrower
}

void read() throws Exception {               // ❌ Invalid — broader
}

A child method cannot declare a broader checked exception than its parent.

#JVM Execution Flow

Java
throw new IllegalArgumentException();
Exception object created
Current method stops
Stack unwinding
Search catch block
Caller
JVM (if never caught)
We'll study stack unwinding in detail in the next chapter

throw Inside catch

Java
try {
    readFile();
} catch (IOException e) {
    throw new RuntimeException(e);
}

This is very common in enterprise applications.

#Spring Boot and Enterprise Usage

Spring Boot Example

Java
public User findUser(Long id) {
    return repository.findById(id)
            .orElseThrow(() -> new UserNotFoundException("User not found"));
}

Internally this does throw new UserNotFoundException("User not found");. No throws required because it extends RuntimeException.

Enterprise Example

Service layer:

Java
public void uploadFile() {
    try {
        storage.upload();
    } catch (IOException e) {
        throw new FileUploadException("Unable to upload file", e);
    }
}

Controller layer:

Java
@ExceptionHandler(FileUploadException.class)

This approach translates low-level exceptions into business-specific exceptions.

#Common Mistakes

Using throws Exception

Java
public void save() throws Exception {   // Wrong
}

public void save() throws IOException { // Better
}

Declare the most specific exception possible.

Throwing Generic Exceptions

Java
throw new Exception("Error");                                    // Wrong
throw new InvalidOrderException("Order is already shipped");     // Better

Losing the Original Cause

Java
// Wrong
catch (IOException e) {
    throw new RuntimeException("Upload Failed");
}

// Correct
catch (IOException e) {
    throw new RuntimeException("Upload Failed", e);
}

Always preserve the original exception.

#Interview Questions

Q1What is the difference between throw and throws?
  • throw actually throws an exception object.
  • throws declares that a method may throw one or more exceptions.
Q2Can we use throw without throws?

Yes, for unchecked exceptions — e.g. throw new IllegalArgumentException();

Q3Can we use throws without throw?

Yes. A method can declare an exception even if it does not currently throw it. This is useful when implementations or subclasses may throw that exception.

Q4Can constructors use throws?

Yes. Constructors may declare checked exceptions.

Q5Can one method declare multiple exceptions?

Yes — e.g. throws IOException, SQLException.

Q6Can one throw statement throw multiple exceptions?

No. Each throw statement throws exactly one exception object.

Q7Is throw null valid?

Yes. It compiles, but the JVM throws a NullPointerException at runtime.

Q8Is throws RuntimeException required?

No. Unchecked exceptions do not need to be declared.

Q9Which keyword is used more often in Spring Boot?
  • throw is used frequently for business exceptions.
  • throws is mainly used for checked exceptions from I/O or external libraries.
Q10Why should we preserve the original exception?

To retain the root cause and complete stack trace, making debugging much easier.

#Best Practices

  • Use throw for business validations and custom exceptions.
  • Use throws only when callers can meaningfully handle checked exceptions.
  • Declare the most specific checked exception possible.
  • Preserve the original cause when wrapping exceptions.
  • Prefer domain-specific exceptions over generic ones.
  • Avoid throws Exception in public APIs unless absolutely necessary.

#Chapter Summary

  • The throw and throws keywords and their differences
  • Checked vs unchecked behaviour
  • Multiple exceptions
  • Re-throwing and wrapping exceptions
  • Constructor and interface usage
  • Method overriding rules
  • Spring Boot usage and enterprise best practices