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
En esta página del sitio puede ver el video en línea [Old Version]User Defined Exception in Python || Lesson 49 || Python || Learning Monkey || de Duración hora minuto segunda en buena calidad , que subió el usuario Wisdomers - Computer Science and Engineering 23 marzo 2021, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 3,331 veces y le gustó 42 a los espectadores. Disfruta viendo!