-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtry.py
More file actions
22 lines (21 loc) · 773 Bytes
/
try.py
File metadata and controls
22 lines (21 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def divide_numbers(a, b):
try:
# Attempt to divide a by b
result = a / b
print(f"The result is {result}")
except ZeroDivisionError:
# Handle division by zero error
print("Error: Cannot divide by zero.")
except TypeError:
# Handle incorrect data types error
print("Error: Both inputs must be numbers.")
else:
# This block runs if no exceptions were raised
print("Division successful.")
finally:
# This block always runs, even if an exception occurred
print("End of division operation.")
# Testing divide_numbers function
divide_numbers(10, 2) # Normal division
divide_numbers(10, 0) # Division by zero
divide_numbers(10, 'a') # Type error