0
0
Pythonprogramming~5 mins

Try–except execution flow in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of a try block in Python?
The try block lets you test a block of code for errors. If an error occurs, Python stops running the try block and looks for an except block to handle the error.
Click to reveal answer
beginner
What happens when an exception occurs inside a try block?
Python immediately stops executing the try block and jumps to the matching except block to handle the error. If no matching except block is found, the program crashes.
Click to reveal answer
beginner
Can code after the error inside a try block run if an exception occurs?
No. Once an exception happens, the rest of the try block is skipped. The program moves to the except block.
Click to reveal answer
intermediate
What is the role of the else block in a try-except structure?
The else block runs only if no exceptions were raised in the try block. It is useful for code that should run only when everything in try succeeds.
Click to reveal answer
intermediate
How does the finally block behave in a try-except structure?
The finally block always runs after the try and except blocks, no matter what. It is used for cleanup actions like closing files or releasing resources.
Click to reveal answer
What happens if no exception occurs in the try block and there is an except block?
AThe <code>try</code> block runs again.
BThe <code>except</code> block runs anyway.
CThe program crashes.
DThe <code>except</code> block is skipped.
Where does Python jump when an exception occurs inside a try block?
ATo the <code>except</code> block.
BTo the next line in the <code>try</code> block.
CTo the <code>else</code> block.
DTo the <code>finally</code> block only.
When does the else block run in a try-except structure?
AAlways, before the <code>try</code> block.
BOnly if an exception occurs.
COnly if no exception occurs.
DOnly if the <code>finally</code> block is missing.
What is guaranteed about the finally block?
AIt runs only if an exception occurs.
BIt always runs, no matter what.
CIt runs only if no exception occurs.
DIt runs only if there is an <code>else</code> block.
If an exception is not caught by any except block, what happens?
AThe program crashes and shows an error.
BThe <code>else</code> block runs.
CThe program continues normally.
DThe <code>finally</code> block is skipped.
Explain the flow of execution in a try-except-else-finally structure when an exception occurs.
Think about which blocks run and which are skipped when an error happens.
You got /6 concepts.
    Describe what happens when no exception occurs in a try-except-else-finally structure.
    Focus on the normal flow without errors.
    You got /4 concepts.