Try-except-else helps you handle errors in your code safely. It lets you run special code if no errors happen.
When you want to try a task that might fail, like opening a file.
When you want to run some code only if no errors occurred.
When you want to separate error handling from normal code.
When you want to keep your program running even if something goes wrong.
Syntax
Python
try:
# code that might cause an errorexcept SomeError:
# code to handle the errorelse:
# code to run if no error happened
The else block runs only if the try block did not raise any error.
If an error happens, the else block is skipped.
Examples
This example divides 10 by 2. Since no error happens, the else block prints the result.
Python
try:
x = 10 / 2except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Division successful, result is", x)
This example tries to divide by zero, which causes an error. The except block runs, and the else block is skipped.
Python
try:
x = 10 / 0except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Division successful, result is", x)
Sample Program
This program defines a function to divide two numbers. It uses try-except-else to handle division errors. If division works, it prints the result. If division by zero happens, it prints an error message.
Python
def divide_numbers(a, b):
try:
result = a / b
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
else:
print(f"Division successful: {a} / {b} = {result}")
print("Test 1:")
divide_numbers(10, 2)
print("Test 2:")
divide_numbers(10, 0)
OutputSuccess
Important Notes
The else block is useful to keep the code that runs only when no errors happen separate and clear.
Use except to catch specific errors you expect, so you don't hide other bugs.
Summary
try runs code that might fail.
except runs if an error happens.
else runs only if no error happened.
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.