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-else 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 nicely and also do something special when there is no error.
🎯 Goal: You will create a program that tries to divide two numbers, catches division errors, and uses else to print a success message only when no error happens.
📋 What You'll Learn
Create two variables numerator and denominator with exact values
Create a variable result and set it to None
Use a try block to divide numerator by denominator and store in result
Use an except ZeroDivisionError block to set result to the string 'Cannot divide by zero'
Use an else block to print 'Division successful'
Finally, print the value of result
💡 Why This Matters
🌍 Real World
Handling errors like division by zero is common in calculators, data processing, and user input validation.
💼 Career
Understanding try-except-else helps you write robust code that doesn't crash and gives clear feedback, a key skill for any programmer.
Progress0 / 4 steps
1
Create numerator and denominator variables
Create a variable called numerator and set it to 10. Create another variable called denominator and set it to 2.
Python
Hint
Use simple assignment to create the two variables with the exact numbers.
2
Create a result variable
Create a variable called result and set it to None.
Python
Hint
Initialize result to None before the try block.
3
Write try-except-else to divide numbers
Write a try block where you divide numerator by denominator and assign it to result. Add an except ZeroDivisionError block that sets result to the string 'Cannot divide by zero'. Add an else block that prints 'Division successful'.
Python
Hint
Use the exact keywords and variable names as shown. The else block runs only if no error happens.
4
Print the result
Write a print(result) statement to display the value of result.
Python
Hint
The program should print Division successful first, then the number 5.0.
Practice
(1/5)
1. What does the else block do in a try-except-else structure?
easy
A. Runs only if no error occurs in the try block
B. Runs only if an error occurs in the try block
C. Always runs regardless of errors
D. Runs before the try block
Solution
Step 1: Understand try-except-else flow
The try block runs code that might cause an error. If an error happens, the except block runs.
Step 2: Role of else block
The else block runs only if no error occurs in the try block, meaning the code succeeded without exceptions.
Final Answer:
Runs only if no error occurs in the try block -> Option A
Quick Check:
else runs if no error = A [OK]
Hint: Else runs only when try succeeds without errors [OK]
Common Mistakes:
Thinking else runs after except
Assuming else runs always
Confusing else with finally
2. Which of the following is the correct syntax for a try-except-else block in Python?
easy
A. try:
pass
finally:
pass
else:
pass
B. try:
pass
else:
pass
except:
pass
C. try:
pass
except:
pass
else:
pass
D. except:
pass
try:
pass
else:
pass
Solution
Step 1: Recall correct order of blocks
The correct order is try, then except, then else. The else block must come after except.
Step 2: Check each option
try:
pass
except:
pass
else:
pass follows the correct order and syntax. Options A, B, and D have wrong order or misplaced blocks.