Errors are a normal part of programming—just like mistakes are part of everyday life. What matters is how you handle them. In Python, exception handling helps you manage errors gracefully without crashing your program.
This guide will walk you through exception handling in a simple, beginner-friendly way, with real-life examples you can relate to.
What is an Exception?
An exception is an error that occurs while your program is running.
Example:
print(10 / 0)
This will crash your program with:
ZeroDivisionError: division by zero
Instead of letting your program break, you can handle this situation properly.
Basic Structure of Exception Handling?
Python uses the try-except block:
try:
# code that might cause an error
except:
# code that runs if an error occurs
Example:
try:
num = int(input("Enter a number: "))
print(10 / num)
except:
print("Something went wrong!")
Real-Life Analogy.
Think of it like this:
- Try: You try to ride a bike.
- Exception: You fall.
- Except: You handle it (get up, clean yourself).
Handling Specific Exceptions?
Instead of catching all errors, you can handle specific ones:
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("You cannot divide by zero!")
except ValueError:
print("Invalid input! Please enter a number.")
The else Block.
The else block runs if no exception occurs.
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input!")
else:
print("You entered:", num)
The finally Block.
The finally block always runs, whether an error occurs or not.
try:
file = open("data.txt")
except FileNotFoundError:
print("File not found!")
finally:
print("Closing the program.")
Everyday Examples of Exception Handling?
1. ATM Withdrawal.
try:
balance = 5000
amount = int(input("Enter withdrawal amount: "))
if amount > balance:
raise Exception("Insufficient balance!")
print("Withdrawal successful")
except Exception as e:
print("Error:", e)
Real life: If you try to withdraw more money than you have, the system stops you.
2. Online Form Filling.
try:
age = int(input("Enter your age: "))
except ValueError:
print("Please enter a valid number!")
Real life: Entering text instead of numbers in a form shows an error.
3. Opening a File.
try:
f = open("myfile.txt")
content = f.read()
except FileNotFoundError:
print("File does not exist!")
Real life: Trying to open a file that is not there.
Raising Exceptions Manually?
You can create your own errors using raise:
age = int(input("Enter your age: "))
if age < 18:
raise Exception("You must be 18 or older!")
Best Practices.
- Catch specific exceptions instead of using a general except.
- Avoid hiding errors silently.
- Use finally for cleanup tasks (like closing files).
- Keep your try block small and focused.
Conclusion.
Exception handling makes your Python programs:
- More robust.
- More user-friendly.
- Less likely to crash unexpectedly.
Just like in everyday life, problems will happen—but handling them smartly makes all the difference.
FAQs.
1. What is exception handling in Python?
Exception handling in Python is a way to handle runtime errors so that the program does not crash and can continue executing smoothly.
2. What is the difference between an error and an exception?
An error is a broader issue in a program, while an exception is a specific type of error that occurs during execution and can be handled using try-except blocks.
3. Can we use multiple except blocks in Python?
Yes, Python allows multiple except blocks to handle different types of exceptions separately.
4. What is the purpose of the finally block?
The finally block is used to execute code that should run no matter what, whether an exception occurs or not, such as closing files or releasing resources.
5. When should we use raise in Python?
The raise keyword is used when you want to manually trigger an exception based on a specific condition in your program.





