You can
create custom exceptions by extending the class Exception.
test.py
class PasswordException(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) password=input("Enter password\n") if(len(password) < 8): raise PasswordException("Minimum password lenght should be 8 characters")
$ python3 test.py Enter password abcde Traceback (most recent call last): File "test.py", line 11, in <module> raise PasswordException("Minimum password lenght should be 8 characters") __main__.PasswordException: 'Minimum password lenght should be 8 characters'
In this
example, the default __init__() of Exception has been overridden. The new
behaviour simply creates the value attribute. This replaces the default behaviour of creating the args attribute.
No comments:
Post a Comment