Explain throw vs throws in Java

๐Ÿ’ก Concept: throw vs throws

The throw and throws keywords in Java are related to exception handling but serve different purposes.

๐Ÿ“˜ Quick Intro

throw is used to explicitly throw an exception, whereas throws declares that a method can throw one or more exceptions.

๐Ÿง  Analogy

Think of throw as tossing a ball (exception) and throws as a warning sign indicating the ball might be thrown.

๐Ÿ”ง Technical Explanation

  • throw is used inside method body to throw an exception object.
  • throws is used in method signature to declare exceptions the method might throw.
  • throw throws a single exception object.
  • throws can declare multiple exceptions separated by commas.
  • Using throws informs caller about possible exceptions.

๐ŸŽฏ Use Cases

  • โœ… Use throw to manually trigger an exception.
  • โœ… Use throws to delegate exception handling to caller.
  • โœ… Improve code clarity and exception propagation.

๐Ÿ’ป Java throw vs throws Example


public void checkAge(int age) throws Exception {
    if (age < 18) {
        throw new Exception("Age must be 18 or older");
    }
}

public void demo() {
    try {
        checkAge(15);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

โ“ Interview Q&A

Q1: What is the difference between throw and throws?
A: throw is used to throw an exception, throws declares exceptions a method might throw.

Q2: Can a method have multiple exceptions in throws?
A: Yes, separated by commas.

Q3: Can throw be used outside method?
A: No.

Q4: Is throws mandatory?
A: Only for checked exceptions.

Q5: Can throw throw multiple exceptions?
A: No, only one at a time.

Q6: Does throws handle exceptions?
A: No, it declares.

Q7: Is throw a keyword or method?
A: Keyword.

Q8: Can throws be used with runtime exceptions?
A: Yes, but optional.

Q9: What happens if exception is not caught?
A: It propagates.

Q10: Can throw be inside try block?
A: Yes.

๐Ÿ“ MCQs

Q1. What is the difference between throw and throws?

  • throw throws an exception, throws declares exceptions
  • throw declares exceptions, throws throws exception
  • Both are same
  • None

Q2. Can a method have multiple exceptions in throws?

  • No
  • Yes
  • Sometimes
  • Only one

Q3. Can throw be used outside method?

  • Yes
  • No
  • Sometimes
  • Only in Java 9

Q4. Is throws mandatory?

  • Always
  • Only for checked exceptions
  • Never
  • Sometimes

Q5. Can throw throw multiple exceptions?

  • Yes
  • No
  • Sometimes
  • Only in Java 11

Q6. Does throws handle exceptions?

  • Yes
  • No
  • Sometimes
  • Only in Java 9

Q7. Is throw a keyword or method?

  • Method
  • Keyword
  • Class
  • Object

Q8. Can throws be used with runtime exceptions?

  • No
  • Yes
  • Sometimes
  • Only checked

Q9. What happens if exception is not caught?

  • It is ignored
  • It propagates
  • Program crashes
  • Handled by JVM

Q10. Can throw be inside try block?

  • No
  • Yes
  • Sometimes
  • Only in Java 8

๐Ÿ’ก Bonus Insight

Using throw and throws properly improves exception clarity and control flow in Java.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: