How to use custom exception classes in python
This article shows you how to use custom exception classes in python, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Custom exception class
1. Custom CLA
two。 Inherit the exception base class exception of the system
3. Customize the constructor of the exception class and other methods to handle
For example:
Customize my exception class MyException
This is the simplest exception class.
Class MyException (Exception):
Pass
Case: judging the status of input
If it is not a numeric value, throw an exception.
A=input ("please enter a value?")
If not a.isdigit ():
Raise MyException ("exception: you did not enter a numeric value!")
The summary emphasizes:
1. Custom exception class.
two。 Learn how to take advantage of custom exception classes.
3. The construction method of custom exception class is used to prompt the exception value.
The knowledge source code of this section:
# Custom exception class class MyException (Exception): def _ _ init__ (self,a): self.a=a
# use a=input for the simplest exception class ("Please enter a value?")
If not a.isdigit (): raise MyException (a) the above is how to use custom exception classes in python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.