Like the built-in exceptions of Python, the programmer can also create his own exceptions which are called 'User- defined exceptions' or 'Custom exceptions'.We know Python offers many exceptions which will raise in different contexts. For example, when a number is divided by zero, the ZeroDivisionError is raised. Similarly, when the datatype is not correct, TypeError is raised.
class MyException(Exception): def __init__(self, arg): self.msg = arg
Here, 'MyException' class is the sub class for 'Exception' class.This class has a constructor where a variable 'msg' is defined.This 'msg' receives a message passed from outside through 'arg'.
Here, raise statement is raising MyException class object that contains the given 'message'.
try: code Except MyException as me: print(me)
Here, the object 'me' contains the message given in the raise statement.