This commit is contained in:
lfirmin
2026-01-07 21:42:11 +01:00
parent d02ef5f88a
commit 11040c45b8
25 changed files with 572 additions and 3 deletions
@@ -0,0 +1,56 @@
class Plant:
def __init__(self, name, water, wilting):
self.name = name
self.water = water
self.wilting = wilting
def check_water(self):
if self.water < 5:
raise WaterError("Not enough water in the tank!")
def check_wilting(self):
if self.wilting == 1:
raise PlantError(f"The {self.name} plant is wilting!")
def check_garden(self):
if self.wilting == 1:
raise PlantError(f"The {self.name} plant is wilting!")
if self.water < 5:
raise WaterError("Not enough water in the tank!")
class GardenError(Exception):
pass
class WaterError(GardenError):
pass
class PlantError(GardenError):
pass
tomato = Plant("tomato", 2, 1)
print("=== Custom Garden Errors Demo ===")
print("\nTesting PlantError...")
try:
tomato.check_wilting()
except PlantError as e:
print("Caught PlantError:", e)
print("\nTesting WaterError...")
try:
tomato.check_water()
except WaterError as e:
print("Caught WaterError:", e)
print("\nTesting catching all garden errors...")
try:
tomato.check_garden()
except GardenError as e:
print("Caught GardenError:", e)
print("\nAll custom error types work correctly!")