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?
✗ Incorrect
The
finally block always runs, whether an exception occurred or not.If an exception occurs and is caught, which blocks run?
✗ 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?✗ Incorrect
A return in
finally overrides any return from try or except.Which block handles exceptions?
✗ Incorrect
The
except block catches and handles exceptions.If no exception occurs, which blocks run?
✗ 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.