From fa284d136ba8644eba71b0764edd2c2f462601a2 Mon Sep 17 00:00:00 2001 From: Axy Date: Mon, 12 Jan 2026 12:23:47 +0100 Subject: [PATCH] partial impl --- ex0/ft_first_exception.py | 29 +++++++++++++++++++++++++++++ ex1/ft_different_errors.py | 35 +++++++++++++++++++++++++++++++++++ ex2/ft_custom_errors.py | 25 +++++++++++++++++++++++++ pyproject.toml | 2 ++ 4 files changed, 91 insertions(+) create mode 100644 ex0/ft_first_exception.py create mode 100644 ex1/ft_different_errors.py create mode 100644 ex2/ft_custom_errors.py create mode 100644 pyproject.toml diff --git a/ex0/ft_first_exception.py b/ex0/ft_first_exception.py new file mode 100644 index 0000000..b83fed9 --- /dev/null +++ b/ex0/ft_first_exception.py @@ -0,0 +1,29 @@ +def check_temperature(temp_str: str) -> None: + try: + temp = int(temp_str) + if temp < 0: + print(f"Error: {temp}°C is to cold for plants (min 0°C)") + elif temp > 40: + print(f"Error: {temp}°C is to hot for plants (max 40°C)") + else: + print(f"Temperature {temp}°C is perfect for plants!") + except ValueError: + print(f"Error: '{temp_str}' is not a valid number") + + +def test_temperature_input() -> None: + def test_check(s: str): + print(f"Testing temperature: {s}") + check_temperature(s) + print() + + print("=== Garden Temperature Checker ===\n") + test_check("25") + test_check("abc") + test_check("100") + test_check("-50") + print("All test completed - program didn't crash!") + + +if __name__ == "__main__": + test_temperature_input() diff --git a/ex1/ft_different_errors.py b/ex1/ft_different_errors.py new file mode 100644 index 0000000..e20d1b8 --- /dev/null +++ b/ex1/ft_different_errors.py @@ -0,0 +1,35 @@ +def garden_operations(exception: str): + if exception == "ValueError": + int("abc") + if exception == "ZeroDivisionError": + print(10 / 0) + if exception == "FileNotFoundError": + open("/dev/agilliar") + if exception == "KeyError": + {}["agilliar"] + print("[WARNING] No exception was raised") + + +def test_error_types() -> None: + print("=== Garden Error Type Demo ===") + for error in [ + "ValueError", + "ZeroDivisionError", + "FileNotFoundError", + "KeyError", + ]: + print(f"\nTesting {error}...") + try: + garden_operations(error) + except ( + ValueError, + ZeroDivisionError, + FileNotFoundError, + KeyError, + ) as e: + print(f"Caught {type(e).__name__}: {e}") + print("\nAll error types tested successfully!") + + +if __name__ == "__main__": + test_error_types() diff --git a/ex2/ft_custom_errors.py b/ex2/ft_custom_errors.py new file mode 100644 index 0000000..265d117 --- /dev/null +++ b/ex2/ft_custom_errors.py @@ -0,0 +1,25 @@ +class GardenError(Exception): + def __str__(self) -> str: + return f"General Garden Error: {super().__str__()}" + + +class PlantError(GardenError): + plant: str + + def __init__(self, plant: str) -> None: + self.plant = plant + + def __str__(self) -> str: + return f"The {self.plant} plant is wilting!" + + +class WaterError(GardenError): + def __init__(self): + pass + + def __str__(self) -> str: + return "Not enough water in the tank!" + + +if __name__ == "__main__": + raise GardenError(12) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a8f43fe --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,2 @@ +[tool.black] +line-length = 79 -- 2.52.0