Day 14 – Exception Handling (try / except)
Project: Build a “Safe Calculator” that handles user input errors gracefully.
01. Learning Goal
By the end of this lesson, you will be able to:
- Understand what exceptions (errors) are in Python
- Handle errors using
tryandexceptblocks - Use
elseandfinallyfor structured control flow - Prevent your program from crashing unexpectedly
02. Problem Scenario
When users input wrong data or when files are missing, your program crashes.
Your goal today is to handle exceptions safely and ensure the app continues running smoothly.
03. Step 1 – What is an Exception?
An exception occurs when something goes wrong during execution.
If not handled, it will stop the program.
print(10 / 0) # ZeroDivisionError
numbers = [1, 2, 3]
print(numbers[5]) # IndexError
Examples of common errors:
- Division by zero
- Accessing an invalid index
- File not found
04. Step 2 – Basic try / except Structure
try:
# Code that may cause an error
except:
# Code to run if an error occurs
Example:
try:
x = int(input("Enter a number: "))
print("You entered:", x)
except:
print("That's not a valid number!")
05. Step 3 – Handling Specific Exceptions
You can handle different types of errors separately.
try:
num = int("abc") # Raises ValueError
except ValueError:
print("Cannot convert to integer.")
06. Step 4 – Multiple Exception Types
try:
a = [1, 2, 3]
print(a[5]) # IndexError
print(10 / 0) # ZeroDivisionError
except IndexError:
print("List index out of range!")
except ZeroDivisionError:
print("Cannot divide by zero.")
07. Step 5 – Using else and finally
-
else: Runs only if no exception occurs. -
finally: Always runs, even if an exception occurs.
try:
n = int(input("Enter a number: "))
except ValueError:
print("Invalid input.")
else:
print("Valid number:", n)
finally:
print("Program finished.")
08. Step 6 – Using the Exception Object
You can capture the error message for debugging.
try:
x = 10 / 0
except Exception as e:
print("Error occurred:", e)
09. Step 7 – Practice Examples
Example 1: Prevent Division by Zero
try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
print(a / b)
except ZeroDivisionError:
print("Cannot divide by zero.")
Example 2: File Handling
try:
with open("not_exist.txt", "r", encoding="utf-8") as f:
data = f.read()
except FileNotFoundError:
print("File not found!")
10. Step 8 – Mini Project: Safe Calculator
Create a calculator that safely handles user errors (e.g., wrong input or zero division).
while True:
try:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")
if op == "+":
print("Result:", a + b)
elif op == "-":
print("Result:", a - b)
elif op == "*":
print("Result:", a * b)
elif op == "/":
print("Result:", a / b)
else:
print("Unknown operator!")
except ValueError:
print("Please enter a valid number.")
except ZeroDivisionError:
print("You can’t divide by zero.")
except Exception as e:
print("Unexpected error:", e)
again = input("Try again? (y/n): ")
if again.lower() != "y":
print("Goodbye!")
break
11. Reflection
You have learned how to:
- Prevent crashes with
try / exceptblocks - Handle multiple error types safely
- Use
else,finally, andException as e - Build a Safe Calculator that gracefully manages errors
Next → Day 15 – Functions (def, return, parameters)
Learn how to write reusable code blocks to make your programs modular and clean.
Top comments (0)