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

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup