Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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:
if (age < 18) { throw new IllegalArgumentException("Age must be 18 or older"); }
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
✗ 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?
Athrow
Btry
Cthrows
Dcatch
✗ Incorrect
throws declares exceptions in method signatures; throw actually throws an exception.
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
✗ 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?
AYes
BNo
COnly if they are of the same type
DOnly in Java 17+
✗ Incorrect
A single throw statement throws only one exception object.
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");
✗ 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.
Practice
(1/5)
1.
What does the throw keyword do in Java?
easy
A. It catches an exception and handles it.
B. It sends an exception to stop normal program flow when an error occurs.
C. It declares a method can throw exceptions.
D. It creates a new thread for parallel execution.
Solution
Step 1: Understand the role of throw
The throw keyword is used to send an exception object explicitly when an error happens.
Step 2: Differentiate from other keywords
throw does not catch exceptions (that's catch), nor declare exceptions (that's throws), nor create threads.
Final Answer:
It sends an exception to stop normal program flow when an error occurs. -> Option B
Which of the following is the correct way to throw a new IllegalArgumentException in Java?
?
easy
A. throws new IllegalArgumentException("Invalid argument");
B. throw IllegalArgumentException("Invalid argument");
C. throw new IllegalArgumentException("Invalid argument");
D. throw new IllegalArgumentException;
Solution
Step 1: Check syntax for throwing exceptions
To throw an exception, use throw new ExceptionType("message") with parentheses and semicolon.
Step 2: Identify correct option
throw new IllegalArgumentException("Invalid argument"); uses correct syntax with new, parentheses, and semicolon. Options B and D miss parentheses or new. throws new IllegalArgumentException("Invalid argument"); uses throws which is for method declarations, not throwing.
Final Answer:
throw new IllegalArgumentException("Invalid argument"); -> Option C
Quick Check:
throw + new + parentheses = A [OK]
Hint: Throw exceptions with 'throw new ExceptionType()' syntax [OK]
Common Mistakes:
Omitting 'new' keyword
Using 'throws' instead of 'throw'
Missing parentheses after exception class
3.
What will be the output of the following Java code?
public class TestThrow {
public static void main(String[] args) {
try {
throw new RuntimeException("Error happened");
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
}
medium
A. Error happened
B. RuntimeException
C. Compilation error
D. No output
Solution
Step 1: Analyze the try block
The code throws a new RuntimeException with message "Error happened".
Step 2: Analyze the catch block
The catch block catches the exception and prints its message using e.getMessage(), which is "Error happened".
Final Answer:
Error happened -> Option A
Quick Check:
Exception message printed = C [OK]
Hint: Catch prints exception message with getMessage() [OK]
Common Mistakes:
Expecting exception type name instead of message
Thinking code causes compilation error
Assuming no output without catch
4.
Identify the error in the following code snippet:
public class Example {
public static void main(String[] args) {
throw new Exception("Problem");
}
}
medium
A. Missing try-catch block or throws declaration for checked exception.
B. Incorrect exception message format.
C. Cannot throw exceptions in main method.
D. Exception class does not exist.
Solution
Step 1: Identify exception type
The code throws Exception, which is a checked exception in Java.
Step 2: Check handling of checked exceptions
Checked exceptions must be either caught in a try-catch block or declared with throws in the method signature. This code does neither, causing a compile error.
Final Answer:
Missing try-catch block or throws declaration for checked exception. -> Option A
Quick Check:
Checked exceptions need handling = D [OK]
Hint: Checked exceptions require try-catch or throws declaration [OK]
Common Mistakes:
Ignoring checked exception rules
Thinking main cannot throw exceptions
Confusing checked and unchecked exceptions
5.
Consider this method that throws an exception if the input is negative:
public void checkNumber(int num) {
if (num < 0) {
throw new IllegalArgumentException("Negative number not allowed");
}
System.out.println("Number is " + num);
}
How should you call this method safely in your code?
hard
A. Use throw keyword again when calling checkNumber.
B. Call checkNumber without any try-catch because IllegalArgumentException is checked.
C. Declare throws IllegalArgumentException in the calling method and do not catch.
D. Call checkNumber inside a try-catch block catching IllegalArgumentException.
Solution
Step 1: Identify exception type thrown
The method throws IllegalArgumentException, which is an unchecked exception.
Step 2: Decide safe calling practice
Although unchecked exceptions do not require declaration, to handle errors safely, call the method inside a try-catch block catching IllegalArgumentException.
Final Answer:
Call checkNumber inside a try-catch block catching IllegalArgumentException. -> Option D
Quick Check:
Catch unchecked exceptions to handle errors safely = A [OK]
Hint: Catch exceptions even if unchecked for safer code [OK]