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 try block in Python?
The try block contains code that might cause an error. Python runs this code and watches for exceptions (errors) to handle them gracefully.
Click to reveal answer
beginner
What happens when an exception occurs inside a try block?
Python stops running the try block and looks for a matching except block to handle the error. If found, it runs the except block code.
Click to reveal answer
beginner
What is the role of the finally block in a try-except structure?
The finally block runs code no matter what happened before—whether an error occurred or not. It is often used to clean up resources like files or connections.
Click to reveal answer
intermediate
Can the finally block change the program's return value if it contains a return statement?
Yes. If the finally block has a return statement, it overrides any previous return from the try or except blocks.
Click to reveal answer
intermediate
What happens if an exception is not caught by any except block?
If no except block matches the exception, the error propagates up and may crash the program after the finally block runs.
Click to reveal answer
Which block always runs regardless of exceptions?
Aelse
Bexcept
Ctry
Dfinally
✗ Incorrect
The finally block always runs, whether an exception occurred or not.
If an exception occurs and is caught, which blocks run?
Atry and except only
Btry, except, and finally
Cexcept and finally only
Dtry and finally only
✗ Incorrect
When an exception is caught, try runs until the error, then except runs, and finally finally runs.
What happens if the finally block contains a return statement?
AIt causes an error
BIt is ignored
CIt overrides any previous return
DIt runs only if no exception occurs
✗ Incorrect
A return in finally overrides any return from try or except.
Which block handles exceptions?
Aexcept
Bfinally
Ctry
Delse
✗ Incorrect
The except block catches and handles exceptions.
If no exception occurs, which blocks run?
Atry and finally
Bexcept only
Cexcept and finally
Dtry and except
✗ Incorrect
If no exception occurs, try runs fully, then finally runs.
Explain the flow of execution in a try-except-finally structure when an exception occurs.
Think about what happens step-by-step when an error is found.
You got /3 concepts.
Describe what happens if the finally block contains a return statement.
Consider how return statements in finally affect the function's output.
You got /3 concepts.
Practice
(1/5)
1. What does the finally block do in a try-except-finally structure?
easy
A. It always runs, whether an error occurs or not.
B. It runs only if an error occurs.
C. It runs only if no error occurs.
D. It runs before the try block.
Solution
Step 1: Understand the role of try and except
The try block runs code that might cause an error, and except runs only if an error happens.
Step 2: Understand the finally block behavior
The finally block always runs after try and except, no matter if an error occurred or not.
Final Answer:
It always runs, whether an error occurs or not. -> Option A
Quick Check:
finally always runs = A [OK]
Hint: Remember: finally always runs last, no matter what. [OK]
Common Mistakes:
Thinking finally runs only on errors
Confusing except and finally blocks
Believing finally runs before try
2. Which of the following is the correct syntax for a try-except-finally block in Python?
easy
A. except:
pass
try:
pass
finally:
pass
B. try:
pass
finally:
pass
except:
pass
C. try:
pass
except:
pass
finally:
pass
D. try:
pass
except:
pass
else:
pass
Solution
Step 1: Recall the order of blocks
The correct order is try, then except, then finally.
Step 2: Check each option's order
try:
pass
except:
pass
finally:
pass follows the correct order. try:
pass
finally:
pass
except:
pass places finally before except, which is invalid. except:
pass
try:
pass
finally:
pass starts with except, which is wrong. try:
pass
except:
pass
else:
pass uses else but no finally.
Final Answer:
try, except, finally in correct order -> Option C
Quick Check:
try-except-finally order = C [OK]
Hint: Remember order: try, except, then finally. [OK]