throw — actively throws (raises) an exception instance. throws — declares that a method might throw certain exceptions (a contract with callers).
Analogy: throw is like actually calling the fire department because there is a fire. throws is like putting a sign on your door that says "fire may occur here" — it warns people but doesn't start anything.
throw keyword:
new Exception("msg")).throw, the code in that method stops executing.1public void validate(int age) {2 if (age < 0) {3 throw new IllegalArgumentException("Age cannot be negative: " + age);4 }5 if (age < 18) {6 throw new ArithmeticException("Must be 18 or older");7 }8 System.out.println("Valid age: " + age);9}1011// Re-throwing an exception:12public void process() throws IOException {13 try {14 readFile();15 } catch (IOException e) {16 System.err.println("Logging error: " + e.getMessage());17 throw e; // Re-throw to let caller handle it18 }19}
throws keyword:
1// Method declaration with throws2public void readFile(String path) throws IOException, SecurityException {3 File file = new File(path);4 if (!file.exists()) {5 throw new FileNotFoundException("File not found: " + path);6 }7 // Read file...8 FileInputStream fis = new FileInputStream(file);9}1011// Caller must handle OR declare:12public void loadData() {13 try {14 readFile("/data/config.txt");15 } catch (IOException e) {16 System.err.println("Failed to load: " + e.getMessage());17 } catch (SecurityException e) {18 System.err.println("Permission denied");19 }20}
Key differences:
Why it matters:
throws in the signature.throw + throws makes your error handling explicit and testable.Common mistakes:
throw without throws when a checked exception needs to propagate.throws Exception as a catch-all (lazy — narrow it down).throws.