From 15115b4c52bfda0d1cca9fa1155beecbb873ec35 Mon Sep 17 00:00:00 2001 From: yctct Date: Sun, 7 Jun 2026 08:59:04 +0200 Subject: First commit, add all files --- py02/ex3/ft_custom_errors.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 py02/ex3/ft_custom_errors.py (limited to 'py02/ex3/ft_custom_errors.py') 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() -- cgit v1.2.3