← Back to Home

throw Keyword

The throw keyword in Java is used to explicitly create and pass an exception object to the JVM. It allows developers to signal exceptional conditions manually, based on business rules or validation logic. This is a high-frequency interview topic, often confused with throws.

What Is the throw Keyword?

  • Used to explicitly throw an exception
  • Can throw one exception at a time
  • Works with exception objects
  • Transfers control to the nearest matching catch block
throw new ExceptionType("message");
          

Why throw Is Needed

  • Enforce business rules
  • Validate input explicitly
  • Create custom error conditions
  • Stop normal execution when rules are violated

Basic Syntax

throw new ArithmeticException("Invalid operation");
          

Simple Example

void checkAge(int age) {
    if (age < 18) {
        throw new ArithmeticException("Not eligible to vote");
    }
    System.out.println("Eligible to vote");
}
          

✔ Exception raised manually
✔ Control shifts to caller or catch block

Using throw with try-catch

try {
    throw new NullPointerException("Null value");
} catch (NullPointerException e) {
    System.out.println(e.getMessage());
}
          

Output:

Null value
          

throw with Checked Exceptions (Important)

When throwing a checked exception, you must handle or declare it.

void readFile() throws IOException {
    throw new IOException("File not found");
}
          

✔ Must use throws
✔ Compiler enforces handling

throw with Unchecked Exceptions

void divide(int a, int b) {
    if (b == 0) {
        throw new ArithmeticException("Division by zero");
    }
}
          

✔ No throws required
✔ Runtime exception

throw vs throws (Interview Favorite)

Aspect throw throws
Purpose Throws an exception Declares possible exceptions
Used in Method body Method signature
Exceptions One at a time Multiple allowed
Object creation Yes No
Role Cause exception Inform caller

Throwing Custom Exceptions (Preview)

class InvalidAgeException extends Exception {
    InvalidAgeException(String msg) {
        super(msg);
    }
}

throw new InvalidAgeException("Age must be >= 18");
          

✔ Enables domain-specific error handling

Important Rules of throw (Interview-Oriented)

  1. Only Throwable objects can be thrown
  2. Cannot throw multiple exceptions at once
  3. Execution stops immediately after throw
  4. Code after throw is unreachable
throw new Exception();
// System.out.println("Hello"); // ❌ unreachable
          

Common Beginner Mistakes

  • Confusing throw with throws
  • Throwing checked exception without declaring
  • Using throw for normal flow control
  • Throwing generic Exception unnecessarily
  • Forgetting meaningful error messages

Best Practices

  • Throw specific exceptions
  • Use unchecked exceptions for programming errors
  • Use checked exceptions for recoverable conditions
  • Include clear, meaningful messages
  • Avoid throwing Exception or Throwable directly

Interview-Ready Answers

Short Answer

The throw keyword is used to explicitly throw an exception object in Java.

Detailed Answer

In Java, the throw keyword allows developers to manually create and throw an exception based on specific conditions. It is used inside method bodies and transfers control to the nearest matching catch block or caller. Checked exceptions thrown using throw must be declared using throws.

Key Takeaway

throw creates the problem; throws declares the responsibility. Use throw to enforce rules and signal failures explicitly and clearly.