mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-01-27 01:01:59 +00:00
02/01
This commit is contained in:
42
02/ex1/ft_different_errors.py
Normal file
42
02/ex1/ft_different_errors.py
Normal file
@@ -0,0 +1,42 @@
|
||||
def garden_operations(case: str) -> None:
|
||||
if case == "ValueError":
|
||||
int("abc")
|
||||
elif case == "ZeroDivisionError":
|
||||
10 / 0
|
||||
elif case == "FileNotFoundError":
|
||||
open("notexist")
|
||||
elif case == "KeyError":
|
||||
dic = {"test0": "t", "test1": "t"}
|
||||
dic["a"]
|
||||
|
||||
|
||||
def test_error_types():
|
||||
try:
|
||||
garden_operations("ValueError")
|
||||
except ValueError:
|
||||
print("Caught ValueError: invalid literal for int()")
|
||||
|
||||
try:
|
||||
garden_operations("ZeroDivisionError")
|
||||
except ZeroDivisionError:
|
||||
print("Caught ZeroDivisionError: division by zero")
|
||||
|
||||
try:
|
||||
garden_operations("FileNotFoundError")
|
||||
except FileNotFoundError as err:
|
||||
print("Caught FileNotFoundError:", err)
|
||||
|
||||
try:
|
||||
garden_operations("KeyError")
|
||||
except KeyError as err:
|
||||
print("Caught KeyError:", err)
|
||||
|
||||
try:
|
||||
garden_operations("ValueError")
|
||||
except (ValueError, KeyError, ZeroDivisionError, FileNotFoundError):
|
||||
print("Caught an error, but program continues!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_error_types()
|
||||
print("All error types tested successfully!")
|
||||
Reference in New Issue
Block a user