๐ซ Exception Handling in Python#
Mistakes (errors) can happen when your program runs, like dividing by zero or opening a missing file. These errors are called exceptions. Exception handling helps your program handle errors gracefully without crashing, so your app can respond or recover smartly! Letโs learn how to handle exceptions. ๐ก๏ธ
What is an Exception? ๐ค#
An exception is an error detected during program execution that interrupts the normal flow of the program.
Example:
print(5 / 0) # ZeroDivisionError: division by zero
Try and Except Block ๐งฐ#
Use try
to run code that might cause an error and except
to handle the error.
try:
num = int(input("Enter a number: "))
result = 10 / num
print("Result is", result)
except ZeroDivisionError:
print("Error! You canโt divide by zero.")
except ValueError:
print("Invalid input! Please enter a valid integer.")
This catches specific errors and lets the program continue.
Catching All Exceptions ๐#
You can catch any exception (not recommended for all cases but useful sometimes):
try:
# risky code here
except Exception as e:
print("An error occurred:", e)
Finally Block ๐#
Code inside finally
runs no matter what, useful for cleanup (closing files, releasing resources).
try:
file = open("test.txt", "r")
data = file.read()
except FileNotFoundError:
print("File not found!")
finally:
file.close()
print("File closed.")
Raising Exceptions ๐#
You can also raise your own exceptions using raise
.
def check_age(age):
if age < 18:
raise ValueError("Age must be at least 18.")
else:
print("Access granted.")
check_age(15) # Raises ValueError with message
Practice Questions with Solutions ๐#
- Write a program that asks for a number and prints its square. Use exception handling for invalid inputs.
- Solution:
try:
num = int(input("Enter a number: "))
print("Square:", num * num)
except ValueError:
print("Please enter a valid integer!")
- Write a program to read a file safely using
try-except-finally
. - Create a function that raises an exception if a string input is empty.
Step-by-Step Mini Project: Safe Division Calculator ๐งฎ#
Make a calculator that divides two numbers but handles errors gracefully.
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError:
return "Error! Cannot divide by zero."
except TypeError:
return "Error! Inputs must be numbers."
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Division result:", safe_divide(num1, num2))
Checklist for This Chapter โ #
- Used
try
andexcept
to catch errors - Handled specific and general exceptions
- Used
finally
for cleanup - Raised custom exceptions with
raise
Youโre now ready to build robust programs that wonโt crash when things go wrong! Practice catching errors and make your apps user-friendly! ๐