Create new exception in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

8th Apr 2020, 9:19 AM
Lương Nguyễn
Lương Nguyễn - avatar
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))
8th Apr 2020, 9:32 AM
XXX
XXX - avatar
0
Tks a lot your answer XXX it's very helpful
8th Apr 2020, 9:47 AM
Lương Nguyễn
Lương Nguyễn - avatar