3 files changed, +166, -0
017c49emodule-02: add ex{2..4}
@@ -0,0 +1,45 @@
1+def garden_operations(operation_number: int) -> None:
2+ if operation_number == 0:
3+ int("abc")
4+ elif operation_number == 1:
5+ 10 / 0
6+ elif operation_number == 2:
7+ open("not_a_file.txt", "r")
8+ elif operation_number == 3:
9+ "temp" + 25
10+ else:
11+ return
12+
13+
14+def test_error_types() -> None:
15+ for op in [0, 1, 2, 3, 4]:
16+ print(f"Testing operation {op}...")
17+ try:
18+ garden_operations(op)
19+ print("Operation completed successfully!")
20+
21+ except ValueError as error:
22+ print(f"Caught ValueError: {error}")
23+
24+ except ZeroDivisionError as error:
25+ print(f"Caught ZeroDivisionError: {error}")
26+
27+ except FileNotFoundError as error:
28+ print(f"Caught FileNotFoundError: {error}")
29+
30+ except TypeError as error:
31+ print(f"Caught TypeError: {error}")
32+
33+ print("\n=== Multiple Error Catch Example ===\n")
34+
35+ for operation in [0, 3]:
36+ try:
37+ garden_operations(operation)
38+
39+ except (ValueError, TypeError) as error:
40+ print(f"Caught ValueError or TypeError: {error}")
41+
42+ print("\nAll tests completed successfully!")
43+
44+if __name__ == "__main__":
45+ test_error_types()
@@ -0,0 +1,59 @@
1+class GardenError(Exception):
2+ def __init__(self, message: str = "Unknown garden error") -> None:
3+ super().__init__(message)
4+
5+
6+class PlantError(GardenError):
7+ def __init__(self, message: str = "Unknown plant error") -> None:
8+ super().__init__(message)
9+
10+
11+class WaterError(GardenError):
12+ def __init__(self, message: str = "Unknown water error") -> None:
13+ super().__init__(message)
14+
15+
16+def check_plant(plant_status: str) -> None:
17+ if plant_status == "wilting":
18+ raise PlantError("The tomato plant is wilting!")
19+
20+
21+def check_water(water_level: int) -> None:
22+ if water_level < 10:
23+ raise WaterError("Not enough water in the tank!")
24+
25+
26+def test_custom_errors():
27+ print("=== Custom Garden Errors Demo ===\n")
28+
29+ print("Testing PlantError...")
30+ try:
31+ check_plant("wilting")
32+ except PlantError as error:
33+ print(f"Caught PlantError: {error}")
34+
35+ print()
36+
37+ print("Testing WaterError...")
38+ try:
39+ check_water(5)
40+ except WaterError as error:
41+ print(f"Caught WaterError: {error}")
42+
43+ print()
44+
45+ print("Testing catching all garden errors...")
46+ for test in [0, 1]:
47+ try:
48+ if test == 0:
49+ check_plant("wilting")
50+ else:
51+ check_water(5)
52+
53+ except GardenError as error:
54+ print(f"Caught GardenError: {error}")
55+ print("\nAll custom error types work correctly!")
56+
57+
58+if __name__ == "__main__":
59+ test_custom_errors()
@@ -0,0 +1,62 @@
1+class GardenError(Exception):
2+ def __init__(self, message: str = "Unknown garden error") -> None:
3+ super().__init__(message)
4+
5+
6+class PlantError(GardenError):
7+ def __init__(self, message: str = "Unknown plant error") -> None:
8+ super().__init__(message)
9+
10+
11+def water_plant(plane_name: str) -> None:
12+ if plane_name != plane_name.capitalize():
13+ raise PlantError(f"Invalid plant name to water: '{plane_name}'")
14+ print(f"Watering {plane_name}: [OK]")
15+
16+
17+def test_watering_system() -> None:
18+ print("=== Garden Watering System ===\n")
19+
20+ print("Testing valid plants...")
21+
22+ try:
23+ print("Opening watering system")
24+
25+ valid_plants = ["Tomato", "Lettuce", "Carrots"]
26+
27+ for plant in valid_plants:
28+ water_plant(plant)
29+
30+ except PlantError as error:
31+ print(f"Caught PlantError: {error}")
32+ print(".. ending tests and returning to main")
33+ return
34+
35+ finally:
36+ print("Closing watering system")
37+
38+ print()
39+
40+ print("Testing invalid plants...")
41+
42+ try:
43+ print("Opening watering system")
44+
45+ invalid_plants = ["Tomato", "lettuce", "Carrots"]
46+
47+ for plant in invalid_plants:
48+ water_plant(plant)
49+
50+ except PlantError as error:
51+ print(f"Caught PlantError: {error}")
52+ print(".. ending tests and returning to main")
53+ return
54+
55+ finally:
56+ print("Closing watering system")
57+
58+ print("\nCleanup always happens, even with errors!")
59+
60+
61+if __name__ == "__main__":
62+ test_watering_system()