push
This commit is contained in:
@@ -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!")
|
||||
Reference in New Issue
Block a user