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
Try-except-finally behavior
📖 Scenario: Imagine you are writing a small program that divides numbers. Sometimes, the user might enter zero as the divisor, which causes an error. You want to handle this error gracefully and always print a message at the end.
🎯 Goal: You will create a program that divides two numbers, handles division by zero using try and except, and always prints a final message using finally.
📋 What You'll Learn
Create two variables with exact values for dividend and divisor
Create a variable to store the result initialized to None
Use a try-except-finally block to perform division and handle ZeroDivisionError
Print the result or an error message
Always print a final message regardless of error
💡 Why This Matters
🌍 Real World
Handling errors like division by zero is common in programs that do calculations, such as calculators or financial apps.
💼 Career
Understanding try-except-finally is important for writing robust code that doesn't crash unexpectedly, a key skill for any programmer.
Progress0 / 4 steps
1
DATA SETUP: Create variables for dividend and divisor
Create two variables called dividend and divisor with values 10 and 0 respectively.
Python
Hint
Use simple assignment like dividend = 10 and divisor = 0.
2
CONFIGURATION: Create a variable to store the result
Create a variable called result and set it to None.
Python
Hint
Initialize result to None before the try block.
3
CORE LOGIC: Use try-except-finally to divide and handle errors
Write a try block that divides dividend by divisor and assigns it to result. Add an except ZeroDivisionError block that sets result to the string 'Cannot divide by zero'. Add a finally block that prints 'Execution completed'.
Python
Hint
Use try, except ZeroDivisionError, and finally blocks exactly as described.
4
OUTPUT: Print the result
Write a print(result) statement to display the value of result.
Python
Hint
Print the result after the try-except-finally block.
Practice
(1/5)
1. What does the finally block do in a try-except-finally structure?
easy
A. It always runs, whether an error occurs or not.
B. It runs only if an error occurs.
C. It runs only if no error occurs.
D. It runs before the try block.
Solution
Step 1: Understand the role of try and except
The try block runs code that might cause an error, and except runs only if an error happens.
Step 2: Understand the finally block behavior
The finally block always runs after try and except, no matter if an error occurred or not.
Final Answer:
It always runs, whether an error occurs or not. -> Option A
Quick Check:
finally always runs = A [OK]
Hint: Remember: finally always runs last, no matter what. [OK]
Common Mistakes:
Thinking finally runs only on errors
Confusing except and finally blocks
Believing finally runs before try
2. Which of the following is the correct syntax for a try-except-finally block in Python?
easy
A. except:
pass
try:
pass
finally:
pass
B. try:
pass
finally:
pass
except:
pass
C. try:
pass
except:
pass
finally:
pass
D. try:
pass
except:
pass
else:
pass
Solution
Step 1: Recall the order of blocks
The correct order is try, then except, then finally.
Step 2: Check each option's order
try:
pass
except:
pass
finally:
pass follows the correct order. try:
pass
finally:
pass
except:
pass places finally before except, which is invalid. except:
pass
try:
pass
finally:
pass starts with except, which is wrong. try:
pass
except:
pass
else:
pass uses else but no finally.
Final Answer:
try, except, finally in correct order -> Option C
Quick Check:
try-except-finally order = C [OK]
Hint: Remember order: try, except, then finally. [OK]