0
Create new exception in python
class RuntimeError(Exception): def __init__(self, mismatch): Exception.__init__(self, mismatch) try: print ("And now, the Vocational Guidance Counsellor Sketch.") raise RuntimeError("Does not have proper hat") print ("This print statement will not be reached.") except RuntimeError as problem: print ("Vocation problem: {0}".format(problem)) Can u guys help me with this I find this so difficult to understand In line Exception.__init__(self, mismatch) that i can't clearly understand that
2 Answers
+ 3
What you actually doing in here is inheriting from the Exception class. This allows you to make your own user-defined exception. And then the constructor for the user defined exception is defined. And the line
Exception.__init__(self, mismatch)
calls the __init__ method (constructor) for the Exception class, because when you define a constructor in a Child class (class which inherits), it overrides the __init__ method of the Base class.
(If you did not understand what I said, please go through the course here once again (or complete it if you haven't))
0
Tks a lot your answer XXX it's very helpful