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 is the purpose of the throws keyword in Java?
The throws keyword is used in a method signature to declare that the method might throw certain checked exceptions. It tells the caller to handle or further declare these exceptions.
Click to reveal answer
beginner
How does throws differ from throw in Java?
throw is used to actually throw an exception object inside a method, while throws declares that a method might throw exceptions, informing the caller to handle them.
Click to reveal answer
intermediate
Can a method declare multiple exceptions with throws? How?
Yes, a method can declare multiple exceptions by listing them separated by commas after the throws keyword, for example: void myMethod() throws IOException, SQLException.
Click to reveal answer
beginner
What happens if a method does not handle a checked exception and does not declare it with throws?
The Java compiler will produce an error because checked exceptions must be either caught with a try-catch block or declared with throws in the method signature.
Click to reveal answer
intermediate
Is it mandatory to declare unchecked exceptions with throws?
No, unchecked exceptions (like RuntimeException) do not need to be declared with throws. It is optional because they can occur anywhere and are not checked at compile time.
Click to reveal answer
What does the throws keyword do in a Java method?
ACreates a new exception object
BDeclares exceptions that the method might throw
CCatches exceptions inside the method
DPrevents exceptions from occurring
✗ Incorrect
The throws keyword declares exceptions that the method might throw, informing callers to handle them.
Which of these is a correct way to declare multiple exceptions using throws?
Which of the following is the correct way to declare a method that might throw an IOException?
public void readFile() _____ IOException { }
easy
A. thrown
B. throw
C. throws
D. throws new
Solution
Step 1: Recall correct syntax for exception declaration
In Java, the keyword to declare exceptions a method might throw is throws.
Step 2: Check options for syntax correctness
throw is used to actually throw an exception inside method body, not in declaration. thrown and throws new are invalid.
Final Answer:
throws -> Option C
Quick Check:
Method declaration uses throws [OK]
Hint: Method declarations use 'throws', not 'throw' [OK]
Common Mistakes:
Using 'throw' instead of 'throws' in method signature
Adding 'new' after throws
Using non-existent keywords like 'thrown'
3.
What will be the output of the following code?
import java.io.*;
public class Test {
public static void risky() throws IOException {
throw new IOException("Error happened");
}
public static void main(String[] args) {
try {
risky();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
medium
A. Error happened
B. Compilation error due to missing throws
C. No output
D. Runtime error without message
Solution
Step 1: Analyze method throwing exception
The method risky() declares it throws IOException and actually throws it with message "Error happened".
Step 2: Check exception handling in main
The main method calls risky() inside a try block and catches IOException, printing the exception message.
Final Answer:
Error happened -> Option A
Quick Check:
Exception caught and message printed [OK]
Hint: Thrown exceptions must be caught or declared [OK]
Common Mistakes:
Thinking throws causes compile error if caught
Expecting no output because exception thrown
Confusing throws with throw inside method body
4.
Identify the error in the following code snippet:
public void process() {
riskyMethod() throws IOException;
}
medium
A. Incorrect use of throws keyword inside method body
B. Missing try-catch block around riskyMethod() call
C. Method process() should declare throws IOException
D. All of the above
Solution
Step 1: Check syntax of throws usage
The throws keyword cannot be used inside a method body; it belongs in the method signature.
Step 2: Analyze exception handling requirements
Calling riskyMethod() which throws IOException requires either a try-catch block or declaring throws IOException in process().
Step 3: Combine all errors
All these issues are present: wrong throws usage, missing try-catch, and missing throws declaration.
Final Answer:
All of the above -> Option D
Quick Check:
Throws only in signature + handle exceptions [OK]
Hint: Throws keyword only in method signature, not inside body [OK]
Common Mistakes:
Using throws inside method body
Not handling checked exceptions properly
Forgetting to declare throws in method signature
5.
You have a method readData() that calls two other methods: openFile() and parseFile(). Both can throw IOException. How should you declare readData() to properly handle exceptions?
hard
A. Do nothing, exceptions will be handled automatically
B. Declare readData() with throws IOException and let caller handle it
C. Declare readData() with throws Exception to cover all exceptions
D. Use try-catch inside readData() to catch and ignore exceptions
Solution
Step 1: Understand exception propagation
If openFile() and parseFile() throw IOException, readData() must either handle or declare these exceptions.
Step 2: Choose proper declaration
Declaring throws IOException in readData() lets the caller decide how to handle exceptions, keeping code clean and clear.
Step 3: Evaluate other options
Ignoring exceptions is bad practice. Declaring throws Exception is too broad. Exceptions are not handled automatically.
Final Answer:
Declare readData() with throws IOException and let caller handle it -> Option B
Quick Check:
Declare checked exceptions to propagate [OK]
Hint: Declare throws for checked exceptions to pass responsibility [OK]
Common Mistakes:
Ignoring exceptions instead of declaring or catching