PYTHON ADVANCED - 09: EXCEPTIONS
IN THIS TUTORIAL, WE WILL LEARN ABOUT EXCEPTIONS:
NOTE: TRY ALL METHODS/CODES.
# ERRORS and Exceptions
# x = -5
# if x<0:
# raise Exception('x shuld be positive')
# assert (x >= 0), "x is not positive"
# try:
# a = 5/0
# except:
# print("Galti se error")
# except Exception as e:
# print(e)
# except ZeroDivisionError:
# print()
class ValueTooHighError(Exception):
pass
class ValueTooSmallError(Exception):
def __init__(self, message, value):
self.message = message
self.value = value
def test_value(x):
if x>100:
raise ValueTooHighError(" value is too high")
if x>5:
raise ValueTooSmallError("Value is too small")
try:
test_value(1)
except ValueTooHighError as e:
print(e)
except ValueTooSmallError as e:
print(e.message, e.value)
Comments
Post a Comment