Throw vs throws in Java: Key Differences and Usage
throw is used to actually throw an exception object explicitly inside a method, while throws is used in a method signature to declare that the method might throw certain exceptions. throw handles a single exception instance, and throws declares one or more exception types that callers must handle.Quick Comparison
This table summarizes the main differences between throw and throws in Java.
| Aspect | throw | throws |
|---|---|---|
| Purpose | To actually throw an exception object | To declare exceptions a method can throw |
| Usage Location | Inside method body | In method signature after parameter list |
| Number Allowed | One exception object at a time | Multiple exception types separated by commas |
| Effect | Transfers control to exception handler immediately | Informs caller to handle or declare exceptions |
| Type | Used with an instance of Throwable | Used with exception class names |
| Checked Exceptions | Can throw checked or unchecked exceptions | Must declare checked exceptions |
Key Differences
throw is a keyword used inside a method to actually throw an exception object. You create or use an existing exception instance and use throw to pass it to the runtime, which then looks for a matching catch block to handle it.
On the other hand, throws is used in a method declaration to specify that this method might throw certain exceptions. It does not throw exceptions itself but tells the caller that they must handle or further declare these exceptions. This is mainly for checked exceptions that the compiler enforces.
In summary, throw is about throwing an actual exception object, while throws is about declaring possible exceptions a method can throw to the caller.
Code Comparison
Here is an example using throw to throw an exception inside a method.
public class ThrowExample { public static void checkAge(int age) { if (age < 18) { throw new IllegalArgumentException("Age must be 18 or older"); } else { System.out.println("Access granted"); } } public static void main(String[] args) { checkAge(16); } }
throws Equivalent
This example shows throws used in a method signature to declare a checked exception that the caller must handle.
import java.io.*; public class ThrowsExample { public static void readFile(String fileName) throws IOException { FileReader file = new FileReader(fileName); BufferedReader fileInput = new BufferedReader(file); System.out.println(fileInput.readLine()); fileInput.close(); } public static void main(String[] args) { try { readFile("test.txt"); } catch (IOException e) { System.out.println("File not found or error reading file"); } } }
When to Use Which
Choose throw when you want to actually create and throw an exception object inside a method, especially for signaling an error immediately.
Choose throws when you want to declare that your method might throw certain checked exceptions, so callers know they must handle or declare them. This helps with compile-time safety.
In short, use throw to throw exceptions, and throws to declare them.
Key Takeaways
throw throws an actual exception object inside a method.throws declares exceptions a method can throw to its caller.throw is used with an exception instance; throws lists exception classes.throws for checked exceptions to enforce handling at compile time.throw to signal errors immediately by throwing exceptions.