Recall & Review
beginner
What does the
throw keyword do in Java?The
throw keyword is used to explicitly throw an exception from a method or block of code. It signals that an error or unusual condition has occurred.Click to reveal answer
beginner
How is
throw different from throws in Java?throw is used to actually throw an exception object, while throws is used in a method signature to declare that the method might throw certain exceptions.Click to reveal answer
beginner
Can you throw any object using
throw in Java?No, you can only throw objects that are instances of
Throwable or its subclasses, such as Exception or Error.Click to reveal answer
beginner
What happens after an exception is thrown using
throw?The normal flow of the program stops, and Java looks for a matching
catch block to handle the exception. If none is found, the program terminates.Click to reveal answer
beginner
Show a simple example of using
throw to throw an exception.Example:<br>
if (age < 18) {<br> throw new IllegalArgumentException("Age must be 18 or older");<br>}Click to reveal answer
What type of object can be thrown using the
throw keyword?✗ Incorrect
Only objects that inherit from Throwable, such as Exception or Error, can be thrown.
Which keyword is used to declare exceptions a method might throw?
✗ Incorrect
throws declares exceptions in method signatures; throw actually throws an exception.What happens immediately after an exception is thrown with
throw?✗ Incorrect
Java searches for a catch block that matches the thrown exception to handle it.
Can you throw multiple exceptions at once using a single
throw statement?✗ Incorrect
A single
throw statement throws only one exception object.Which of the following is a correct way to throw an exception?
✗ Incorrect
The correct syntax to throw an exception is using
throw followed by a new exception object.Explain how the
throw keyword works in Java and when you would use it.Think about signaling errors manually in your code.
You got /4 concepts.
Describe the difference between
throw and throws in Java exception handling.One is for declaring, the other is for actually throwing.
You got /4 concepts.