How to custom catch errors in python

**Exception catch: **

try: 
 XXXXX1
 raise Exception(“xxxxx2”) 
except (Exception1,Exception2,……): 
 xxxx3
else:
 xxxxx4
finally:
 xxxxxxx5
  1. The raise statement can customize the error message, as above.

  2. The statement after raise will not be executed, because the exception has been thrown, the control flow will jump to the exception capture module.

  3. The except statement can carry multiple exceptions after one except, or it can use multiple statements to catch multiple exceptions and handle them differently.

  4. If the exception caught by the except statement does not occur, the statement block in the except statement will not be executed. But execute the statement in else

  5. In the above statement, the order of try/except/else/finally must be try– except X– except– else– finally, that is, all except must be before else and finally, and else (if any) must be before finally , And except X must be before except. Otherwise, a syntax error will occur.

  6. Both else and finally are optional.

  7. In the above complete statement, the existence of the else statement must be based on the except X or the except statement. If the else statement is used in a try block without an except statement, a syntax error will occur.

Abnormal parameter output:

try:testRaise()
except PreconditionsException as e: #The wording of python3 must use asprint(e)

For custom exceptions, you only need to customize the exception class to inherit the parent class Exception. In the custom exception class, override the init method of the parent class.

classDatabaseException(Exception):
 def __init__(self,err='Database error'):
 Exception.__init__(self,err)classPreconditionsException(DatabaseException):
 def __init__(self,err='PreconditionsErr'):
 DatabaseException.__init__(self,err)
def testRaise():
 raise PreconditionsException()try:testRaise()
except PreconditionsException as e:print(e)

Note: PreconditonsException is a subclass of DatabaseException.

So if you raise PreconditionException, you can catch both exception classes.

However, if it is raise DatabaseException, PreconditonsException cannot be caught.

Example supplement:

Python custom exception capture exception handling exception

def set_inf(name,age):if not 0< age <120:
 raise ValueError('Out of range')else:print('%s is %s years old'%(name,age))
def set_inf2(name,age):
 assert 0< age <120,'Out of range'print('%s is %s years old'%(name,age))if __name__ =='__main__':try:set_inf('bob',200)
 except ValueError as e:print('Invalid value:',e)set_inf2('bob',200)

This is the end of this article on how python custom captures errors. For more relevant python custom error capture methods, please search ZaLou.Cn

Recommended Posts

How to custom catch errors in python
How to view errors in python
How to add custom modules in Python
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to write try statement in python
How to define private attributes in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
How to use the zip function in Python
How to program based on interfaces in Python
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to install php7.3 in centos8 custom directory
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
How to create a Python virtual environment in Ubuntu 14.04
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
How to install Helm in Ubuntu
python how to view webpage code
How to use hanlp in ubuntu
How to write python configuration file
How to install PHP7.4 in CentOS
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8