0
0
Javaprogramming~5 mins

Throw keyword in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly primitive types
BAny object
COnly String objects
DOnly objects of type Throwable or its subclasses
Which keyword is used to declare exceptions a method might throw?
Athrow
Btry
Cthrows
Dcatch
What happens immediately after an exception is thrown with throw?
AThe program looks for a matching catch block
BThe exception is ignored
CThe program continues normally
DThe exception is automatically handled
Can you throw multiple exceptions at once using a single throw statement?
AYes
BNo
COnly if they are of the same type
DOnly in Java 17+
Which of the following is a correct way to throw an exception?
Athrow new Exception("Error occurred");
Bthrows new Exception("Error occurred");
Cthrow Exception("Error occurred");
Dcatch new Exception("Error occurred");
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.