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 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.
✗ Incorrect
If no error happens, Python skips the except block and continues normally.
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.
✗ Incorrect
Python stops the try block and jumps to the matching except block to handle the error.
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.
✗ Incorrect
The else block runs only if the try block finishes without errors.
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.
✗ Incorrect
The finally block always runs after try and except, even if the program crashes.
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.
✗ Incorrect
If no except block matches the error, the program stops and shows the error message.
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.
Practice
(1/5)
1. What is the main purpose of using a try-except block in Python?
easy
A. To speed up the program execution
B. To handle errors and prevent the program from crashing
C. To repeat a block of code multiple times
D. To define a new function
Solution
Step 1: Understand the role of try block
The try block contains code that might cause an error during execution.
Step 2: Understand the role of except block
The except block catches and handles the error so the program does not stop abruptly.
Final Answer:
To handle errors and prevent the program from crashing -> Option B
Quick Check:
Try-except handles errors = B [OK]
Hint: Try-except blocks catch errors to keep programs running [OK]
Common Mistakes:
Thinking try-except speeds up code
Confusing try-except with loops
Using try-except to define functions
2. Which of the following is the correct syntax to catch a ZeroDivisionError in Python?
easy
A. try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero')
B. try:
x = 1/0
catch ZeroDivisionError:
print('Cannot divide by zero')
C. try:
x = 1/0
except:
print('Error')
finally ZeroDivisionError:
D. try:
x = 1/0
except ZeroDivisionError then:
print('Cannot divide by zero')
Solution
Step 1: Identify correct try-except syntax
Python uses try: followed by except ExceptionType: to catch errors.
Step 2: Check each option for syntax errors
try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero') uses correct except ZeroDivisionError: syntax; others use invalid keywords like catch or incorrect formatting.
Final Answer:
try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero') -> Option A
Quick Check:
Correct except syntax = A [OK]
Hint: Use 'except ExceptionType:' to catch specific errors [OK]
The except line must end with a colon ':' to define the block.
Step 2: Identify missing colon
In the code, except ValueError is missing the colon, causing a syntax error.
Final Answer:
Missing colon ':' after except ValueError -> Option C
Quick Check:
except line needs ':' = A [OK]
Hint: Always put ':' after except ExceptionType [OK]
Common Mistakes:
Forgetting colon after except
Adding parentheses after except
Misordering try and except blocks
5. You want to safely convert user input to an integer, using try-except-else and catching ValueError specifically, printing 'Invalid input' for invalid input and 'Input is', num for valid input, without stopping the program. Which code does this?
hard
A. try:
num = int(input('Enter number: '))
except ValueError:
print('Invalid input')
else:
print('Input is', num)
B. try:
num = int(input('Enter number: '))
except:
print('Invalid input')
else:
print('Input is', num)
C. try:
num = int(input('Enter number: '))
except ValueError:
print('Invalid input')
finally:
print('Done')
D. try:
num = int(input('Enter number: '))
except ValueError:
print('Invalid input')
Solution
Step 1: Understand try-except-else structure
The try block attempts conversion; except handles errors; else runs if no error occurs.
Step 2: Check which option prints 'Invalid input' on error and shows input if valid
try:
num = int(input('Enter number: '))
except ValueError:
print('Invalid input')
else:
print('Input is', num) correctly prints 'Invalid input' on ValueError and prints the number if conversion succeeds.
Final Answer:
try:
num = int(input('Enter number: '))
except ValueError:
print('Invalid input')
else:
print('Input is', num) -> Option A
Quick Check:
Use except for errors, else for success = D [OK]
Hint: Use except for errors and else for success actions [OK]