0
0
Pythonprogramming~5 mins

Try–except–else 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).
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.
Click to reveal answer
intermediate
What is the role of the else block in a try–except–else structure?
The else block runs only if the try block did NOT raise any exceptions. It is for code that should run when everything goes well.
Click to reveal answer
intermediate
When does the else block NOT run in a try–except–else structure?
The else block does NOT run if an exception was raised in the try block, even if the exception was caught by except.
Click to reveal answer
advanced
Why use else instead of putting code after the try-except blocks?
Using else clearly shows which code runs only when no errors happen. It keeps the code organized and avoids accidentally running code after an error.
Click to reveal answer
What part of try–except–else runs only if no error occurs in the try block?
Aelse
Bexcept
Cfinally
Dtry
If an exception occurs in the try block and is caught, does the else block run?
AYes, always
BOnly if the exception is not caught
COnly if the exception is ValueError
DNo, never
Where should you put code that must run regardless of errors?
Atry block
Bfinally block
Celse block
Dexcept block
What happens if no except block matches the exception raised in try?
AThe except block runs anyway
BThe else block runs
CThe program crashes with the error
DThe finally block is skipped
Why might you use an else block after try-except?
ATo run code only if no error happened
BTo catch exceptions
CTo run code only if an error happened
DTo always run code
Explain the flow of execution in a try–except–else structure when no exceptions occur.
Think about what happens when everything goes smoothly.
You got /3 concepts.
    Describe what happens when an exception is raised and caught in the try–except–else structure.
    Focus on how the program handles errors.
    You got /3 concepts.