summaryrefslogtreecommitdiff
path: root/py02/ex3
diff options
context:
space:
mode:
authoryctct <yctct@yctct.com>2026-06-07 08:59:04 +0200
committeryctct <yctct@yctct.com>2026-06-07 08:59:04 +0200
commit15115b4c52bfda0d1cca9fa1155beecbb873ec35 (patch)
treeb3f0975e63eb04dcba732a78ce9bd9abda8acf01 /py02/ex3
First commit, add all files
Diffstat (limited to 'py02/ex3')
-rwxr-xr-xpy02/ex3/ft_custom_errors.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/py02/ex3/ft_custom_errors.py b/py02/ex3/ft_custom_errors.py
new file mode 100755
index 0000000..57d5a47
--- /dev/null
+++ b/py02/ex3/ft_custom_errors.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+class GardenError(Exception):
+ def __init__(self, message="Unknown plant error"):
+ self.message = message
+ # Why self.message; why not just 'message'
+ super().__init__(self.message)
+
+
+class PlantError(GardenError):
+ pass
+
+
+class WaterError(GardenError):
+ pass
+
+
+def test_plant_error(veg_item):
+ if veg_item not in veg_list:
+ raise PlantError(f"The {veg_item} is not in the list!")
+ else:
+ print("The vegetale is in the list")
+
+
+def test_water_error(level):
+ if level < 10:
+ raise WaterError("Not enough water in the tank!")
+ else:
+ print("We have enough water!")
+
+
+def main():
+ print("== Custom Garden Errors Demo")
+ print()
+ try:
+ print("Testing PlantError...")
+ test_plant_error("spinach")
+ except PlantError as err:
+ print("Caught PlantError:", err)
+ print()
+ try:
+ print("Testing WaterError...")
+ test_water_error(4)
+ except WaterError as err:
+ print("Caught WaterError:", err)
+ try:
+ print()
+ print("Testing catching all garden errors...")
+ test_plant_error("spinach")
+ except GardenError as err:
+ print("Caught GardenError:", err)
+ try:
+ test_water_error(4)
+ except GardenError as err:
+ print("Caught GardenError:", err)
+ finally:
+ print()
+ print("All custom error types work correctly!")
+
+
+if __name__ == "__main__":
+ veg_list = ["carrot", "fennel", "sesame"]
+ main()