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 a try–catch block in Java?
A try–catch block is used to handle exceptions (errors) that may occur during program execution, allowing the program to continue running instead of crashing.
Click to reveal answer
beginner
What happens inside the try block?
The try block contains code that might throw an exception. Java runs this code and if an exception occurs, it jumps to the matching catch block.
Click to reveal answer
beginner
What is the role of the catch block?
The catch block catches and handles the exception thrown in the try block. It lets you define what to do when an error happens.
Click to reveal answer
intermediate
Can you have multiple catch blocks after one try block? Why?
Yes, you can have multiple catch blocks to handle different types of exceptions separately, making error handling more specific and clear.
Click to reveal answer
intermediate
What is the purpose of the finally block in try–catch?
The finally block contains code that always runs after the try and catch blocks, whether an exception occurred or not. It's used for cleanup like closing files.
Click to reveal answer
What keyword starts the block where you write code that might cause an exception?
Atry
Bcatch
Cthrow
Dfinally
✗ Incorrect
The try block is where you put code that might throw an exception.
Which block handles the exception thrown in the try block?
Atry
Bthrow
Cfinally
Dcatch
✗ Incorrect
The catch block catches and handles exceptions.
What happens if an exception is not caught in a try–catch block?
AProgram continues normally
BProgram crashes or stops
CException is ignored
DException is logged automatically
✗ Incorrect
If an exception is not caught, the program usually crashes or stops.
Which block always executes after try and catch, regardless of exceptions?
Afinally
Bcatch
Ctry
Dthrow
✗ Incorrect
The finally block always runs, useful for cleanup.
Can you have multiple catch blocks for one try block?
ANo, only one catch block is allowed
BYes, but only if exceptions are the same type
CYes, to handle different exception types
DNo, catch blocks are optional
✗ Incorrect
Multiple catch blocks let you handle different exceptions separately.
Explain how a try–catch block works in Java and why it is useful.
Think about what happens when an error occurs in your code.
You got /4 concepts.
Describe the purpose of the finally block and give an example of when you might use it.
Consider what you want to happen no matter what, even if an error occurs.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a try-catch block in Java?
easy
A. To create new classes
B. To handle errors and prevent program crashes
C. To declare variables
D. To repeat code multiple times
Solution
Step 1: Understand the role of try block
The try block contains code that might cause an error during execution.
Step 2: Understand the role of catch block
The catch block runs only if an error occurs, allowing the program to handle it gracefully.
Final Answer:
To handle errors and prevent program crashes -> Option B
Quick Check:
try-catch handles errors = D [OK]
Hint: Try-catch is for error handling, not loops or declarations [OK]
Common Mistakes:
Confusing try-catch with loops
Thinking try-catch declares variables
Assuming try-catch creates classes
2. Which of the following is the correct syntax to catch an exception in Java?
easy
A. try { /* code */ } catch (e) { /* handle */ }
B. try { /* code */ } catch Exception e { /* handle */ }