Hello Wisdomers, this course is the old version. The teacher you trust has made a brand new, even better version for you. For Complete Playlist Click here to watch it.
• Python For Beginners
#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
User Defined Exception in Python
In this class, we discuss User Defined Exception in Python.
User-Defined Exception
The reader should have prior knowledge of Exceptions. Click here.
Take an example and understand the concept of the user-defined Exception.
In our last class, we discussed that in all the exception classes, the base class is Exception.
To create a user-defined Exception, we have to inherit the class Exception.
class MarksNotinRange(Exception):
def __init__(self, Marks, message="Marks not in (0, 100) range"):
self.marks = Marks
self.message = message
super().__init__(self.message)
marks = int(input("Enter marks: "))
if not 0 lt salary lt 100:
raise MarksNotinRange(marks)
In the above program, we defined the class MarksNotinRange.
The class MarksNotinRange is inheriting the class Exception.
We defined a constructor, and it is taking marks.
The default message is taken in the constructor, and the message is given to the superclass constructor.
The constructor in the class Exception is assigned with the message given in class marks, not in range.
Similarly, the inbuilt python exceptions are also using separate classes for different exceptions, and the messages are given in each class.
We had seen the messages given by inbuilt exceptions in our previous class.
We use the keyword raise to call an exception. It is shown in the program above.
Example to Catch User-Defined Exception
class MarksNotinRange(Exception):
def __init__(self, Marks, message="Marks not in (0, 100) range"):
self.marks = Marks
self.message = message
super().__init__(self.message)
marks = int(input("Enter marks: "))
try:
if not 0 lt salary lt 100:
raise MarksNotinRange(marks)
except MarksNotinRange:
print("marks not matching")
In the above example, we defined an except block to catch the Exception.
Analyze the code above for better practice. The basics are discussed in the previous class.
Link for playlists:
/ @wisdomerscse
Link for our website: https://learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ learningmonkey01@gmail.com
On this page of the site you can watch the video online [Old Version]User Defined Exception in Python || Lesson 49 || Python || Learning Monkey || with a duration of hours minute second in good quality, which was uploaded by the user Wisdomers - Computer Science and Engineering 23 March 2021, share the link with friends and acquaintances, this video has already been watched 3,331 times on youtube and it was liked by 42 viewers. Enjoy your viewing!