0
0
Pythonprogramming~5 mins

Try–except–finally behavior in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
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
Which block handles exceptions?
Aexcept
Bfinally
Ctry
Delse
If no exception occurs, which blocks run?
Atry and finally
Bexcept only
Cexcept and finally
Dtry and except
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.