mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-01-27 01:01:59 +00:00
02/02
This commit is contained in:
33
02/ex2/ft_custom_errors.py
Normal file
33
02/ex2/ft_custom_errors.py
Normal file
@@ -0,0 +1,33 @@
|
||||
class GardenError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return f"Caught a garden error: {self.message}"
|
||||
|
||||
|
||||
class PlantError(GardenError):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return f"Caught PlantError: {self.message}"
|
||||
|
||||
|
||||
class WaterError(GardenError):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return f"Caught WaterError: {self.message}"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise PlantError("The tomato plant is wilting!")
|
||||
except PlantError as err:
|
||||
print(err)
|
||||
try:
|
||||
raise WaterError("Not enough water in the tank!")
|
||||
except WaterError as err:
|
||||
print(err)
|
||||
Reference in New Issue
Block a user