1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: throw is used to explicitly throw an exception object. throws is used in method signatures to declare that a method might throw certain exceptions. throw actually throws; throws just warns callers.
Think of smoke alarms:
throw: Pulling the fire alarm (actually triggering the alarm)throws: Sign saying "Fire alarm installed" (warning it might go off)
2. Conceptual Clarity (The "Simple" Tier)
π‘ The Analogy: The Package Delivery
throw: The delivery person throws the package over your fencethrows: Shipping label warns "Fragileβmay break in transit"
3. Technical Mastery (The "Deep Dive")
Syntax
java
// throw: Inside method body
throw new ExceptionType("message");
// throws: In method signature
void methodName() throws ExceptionType {
// might throw exception
}Key Differences
| Feature | throw | throws |
|---|---|---|
| Location | Inside method body | Method signature |
| Purpose | Actually throw exception | Declare possible exceptions |
| Count | One exception at a time | Multiple (comma-separated) |
| Example | throw new IOException() | void read() throws IOException |
4. Interactive & Applied Code
java
public class ThrowThrowsDemo {
public static void main(String[] args) {
// Example 1: throw
try {
validateAge(15);
} catch (IllegalArgumentException e) {
System.out.println("β " + e.getMessage());
}
// Example 2: throws (must handle or declare)
try {
readFile("data.txt");
} catch (java.io.IOException e) {
System.out.println("β " + e.getMessage());
}
}
// Using throw
static void validateAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("Age must be 18+"); // β
throw
}
System.out.println("β
Valid age");
}
// Using throws (declares exception)
static void readFile(String filename) throws java.io.IOException {
if (!filename.endsWith(".txt")) {
throw new java.io.IOException("Invalid file type"); // throw inside
}
System.out.println("Reading file...");
}
// Rethrowing exceptions
static void processData() throws Exception {
try {
// Some operation
throw new java.io.IOException("IO error");
} catch (java.io.IOException e) {
System.out.println("Logging error...");
throw e; // Rethrow to caller
}
}
}5. The Comparison & Decision Layer
Decision Tree: throw vs throws
graph TD
A{What do you need?}
A -- Create and trigger exception --> B[Use throw]
A -- Warn callers about exceptions --> C[Use throws]
B --> D[throw new Exception]
C --> E[void method throws Exception]
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "Can you override a method and throw a broader exception?" Answer: NO! Overriding methods can:
- β Throw fewer exceptions
- β Throw subclass exceptions
- β NOT throw broader exceptions
java
class Parent {
void method() throws IOException { }
}
class Child extends Parent {
void method() throws FileNotFoundException { } // β
OK (subclass)
// void method() throws Exception { } // β ERROR (broader!)
}Pro-Tip: Use throw for validation:
java
public void setAge(int age) {
if (age < 0) throw new IllegalArgumentException("Negative age");
this.age = age;
}Fail fast! Don't wait for bugs to propagate.