blob: cd64d5fd1528755151ffcf221ef1e18144bd757f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/usr/bin/env python3
# https://docs.python.org/3/tutorial/errors.html#handling-exceptions
def garden_operation(operation_number):
if operation_number == 0:
int("abc")
elif operation_number == 1:
10 / 0
elif operation_number == 2:
open("/file.txt")
elif operation_number == 3:
"string" + 3
else:
print("Operation completed successfully")
def test_error_types():
print("=== Garden Type Demo ===")
for i in range(5):
try:
print(f"Testing operation {i} ...")
garden_operation(i)
except ValueError as err:
print("Caught ValueError", err)
except ZeroDivisionError as err:
print("Caught ZeroDivisionError:", err)
except FileNotFoundError as err:
print("Caught FileNotFoundError:", err)
except TypeError as err:
print("Caught TypeError:", err)
print()
return print("All error types tested successfully!")
if __name__ == "__main__":
test_error_types()
|