Throw and Throws in Java
Java uses two related keywords—throw and throws—that work together to propagate exceptions but serve distinct roles. throw (lowercase, no s) is a state...
Raising Exceptions and Declaring Them
Java uses two related keywords—throw and throws—that work together to propagate exceptions but serve distinct roles. throw (lowercase, no s) is a statement that actually creates and raises an exception object inside method logic. throws (with an s) is a clause in a method signature that advertises which checked exceptions the method may propagate to its callers. Confusing the two is a common beginner mistake; remembering that throw does the action while throws makes the declaration helps keep them straight.
Using throw to Raise Exceptions The throw statement must be followed by an instance of Throwable: throw new IllegalArgumentException("Value cannot be negative"). After throw executes, the method immediately stops and control transfers to the nearest matching catch block in the call stack. You can throw both checked exceptions (IOException, SQLException) and unchecked exceptions (NullPointerException, IllegalStateException). Throwing unchecked exceptions for programming logic errors and checked exceptions for recoverable external conditions is the accepted convention.
Using throws in Method Signatures A method that may throw a checked exception but does not catch it must declare the exception in its signature: public void readConfig(String path) throws IOException. This informs the caller at compile time that they must either catch the IOException or declare it themselves. A method can list multiple exception types separated by commas. Declaring unchecked exceptions in throws is legal but not required and is generally considered unnecessary noise.
Re-Throwing and Exception Wrapping Sometimes a method catches an exception, adds context, and re-throws it. Wrapping is particularly valuable: catch a low-level exception like SQLException and throw a higher-level one like DataAccessException(message, cause). This hides implementation details from the caller while preserving the original exception as the cause, accessible via getCause(). Since Java 7, you can also re-throw the caught exception without losing its specific type by declaring it in the throws clause even when the catch clause is more general.
Code Example: class AgeValidator { static void validate(int age) throws IllegalArgumentException { if (age < 0 || age > 150) { throw new IllegalArgumentException("Invalid age: " + age); } } }
try { AgeValidator.validate(-5); } catch (IllegalArgumentException e) { System.out.println("Caught: " + e.getMessage()); // Caught: Invalid age: -5 }
Interview Tip: A subtle interview question: can you throw a checked exception from a catch block that caught a different checked exception, without declaring it in the method's throws clause? No—if the method does not already declare or catch the new checked exception, the compiler will reject it. This comes up in exception translation scenarios and tests fine-grained knowledge of how the compiler enforces checked exception rules.