HuntExams Academy logo
JavaBeginner#exceptions

What is the difference between throw and throws?

Answer

throw is a statement that actually raises an exception. throws is a method signature clause declaring that the method may propagate certain checked exceptions.

Example

void validate(int age) throws IllegalArgumentException {
  if (age < 0) throw new IllegalArgumentException("Negative age");
}

Detailed explanation

In Java, throw and throws are related to exception handling, but they serve different purposes. throw is an executable statement that throws an exception object at a particular point in the code. throws is a clause in a method or constructor declaration that lists exception types the operation may propagate to its caller. The central distinction is action versus declaration: throw changes execution flow, whereas throws describes an exception contract and does not itself throw anything.

The syntax of throw is throw expression;, where the expression must evaluate to an object whose type is Throwable or a subclass. For example, throw new IllegalArgumentException("Negative age"); creates and throws an exception when validation fails. You can also throw an existing exception object, such as throw e; inside a catch block. Throwing an exception stops normal execution at that point. Java searches for a compatible catch handler, moving through callers if necessary; if none handles it, the affected thread terminates after uncaught-exception handling.

The syntax of throws places exception types after a method’s parameter list, for example, void load() throws IOException, SQLException. For checked exceptions, the compiler generally requires a method to catch them or declare the types that can escape. A caller must then handle or declare those checked exceptions too. A method can need throws without containing a throw statement because a method it calls may throw a checked exception. Conversely, declaring an exception does not mean that every invocation will throw it, and the declaration does not handle the exception.

In your validate example, IllegalArgumentException extends RuntimeException, making it unchecked. Therefore, throws IllegalArgumentException is legal but optional; void validate(int age) with the same body is sufficient. Unchecked exceptions and Errors are exempt from the catch-or-declare requirement. Interviewers usually look for this checked-versus-unchecked distinction, correct syntax, and an understanding that throw uses an exception object while throws lists exception types. Neither keyword is restricted exclusively to checked exceptions.

Key points to remember

  • throw is an executable statement; throws is part of a method or constructor declaration.
  • throw takes a single exception-object expression; throws can list multiple exception types.
  • throw interrupts normal execution and initiates exception propagation.
  • Checked exceptions that can escape generally must be declared; callers must catch or declare them.
  • In the given example, declaring IllegalArgumentException is optional because it is unchecked.

Common mistakes candidates make

  • Saying that throws throws or handles an exception, rather than declaring possible propagation.
  • Claiming that throws can list only checked exceptions or that every exception must be declared.
  • Confusing objects with types: writing throw IOException instead of throwing an object, or putting new IOException() in a throws clause.

Likely follow-up questions

Can a method use throw without a throws clause?

Yes, it can throw an unchecked exception without declaring it. It can also throw a checked exception if that exception is handled within the method rather than escaping.

Can a method declare throws without using throw in its body?

Yes, a checked exception may propagate from another method it calls. A throws clause can also declare an exception even if a particular implementation never throws it.

Can an overriding method change the throws clause?

For checked exceptions, an overriding method can declare fewer exceptions or narrower exception types, but cannot introduce broader or unrelated checked exceptions. Unchecked exceptions are not subject to this restriction.

Interview tip: Start with “throw is the action; throws is the declaration,” then use the example to explain why declaring IllegalArgumentException is optional.

Related Interview Questions

1
JavaIntermediate#exceptions

Explain try-with-resources.

Open
2
JavaIntermediate#fundamentals

What is autoboxing and unboxing?

Open
3
JavaIntermediate#collections

What is the difference between Comparable and Comparator?

Open