Python common exception classification and handling methods, python Processing Methods
Common Python exception typesIt can be divided into the following categories:
1. AssertionError: The exception thrown when the assert asserted condition is false.
2. AttributeError: exception thrown when the accessed object property does not exist
3. IndexError: exception thrown when the range of the object index is exceeded
4. KeyError: An exception thrown when searching for a nonexistent key in the dictionary
5. NameError: exception thrown when accessing a non-existent variable
6. OSError: exceptions caused by the operating system
7. SyntaxError: this exception is thrown when a syntax error occurs.
8. TypeError: type error. It is usually caused by operations between different types.
9. ZeroDivisionError: this exception occurs when the divisor is 0 during a mathematical operation.
For more exceptions, see the official documentation:
Version 2.7
Version 3.6
Python exception handling:
Example 1: The simplest way to handle exceptions
#! /Usr/bin/python # coding: utf8 # combine try and try t usage a = 1 B = 2 try: assert a> B # If expression a> B is false, the AssertionError exception will be thrown when judgment t AssertionError: # If an AssertionError is caught, print ("a <B") runs the code block under Objective T ")
In the above example, the output result is a <B because a> B is false during AssertionError. When this exception is caught, the statement in the blocks T will be executed.
Example 2: capture exceptions using multiple TBS
#! /Usr/bin/python # coding: utf8 # try is used in combination with multiple worker T and executed in sequence in the try code block, stop executing a = 1 B = 2 c = "1" try: assert a <B d = a + c prepare t AssertionError: print ("a <B ") failed t TypeError, e: # Here e is the exception information print (e)
The execution result above is unsupported operand type (s) for +: 'int' and 'str' does not support adding integer and string types. The previous AssertionError is true, so no AssertionError exception occurs, at this time, the following statement is executed, and a TypeError exception occurs. At this time, the code block under the release T TypeError will be executed, and the error information of the following e-generation table exception will be executed, therefore, the result is an error message indicating an exception.
Example 3: Use of try, try, and else
#! /Usr/bin/python # coding: utf8 a = 1 B = 2 c = "1" try: assert a <B d = a + B partition t AssertionError, e: print ("a <B") failed t TypeError, e: print (e) else: # execute the print statement ("Program execution successful") When no exception is found in the try code block ")
The preceding execution result is
Example 4: try and use it with else and finally (else is not available)
#! /Usr/bin/python # coding: utf8 # try is used in combination with multiple worker T and executed in sequence in the try code block, if an exception is caught, stop executing a = 1 B = 2 c = "1" try: assert a <B d = a + B txt = open ("/root/1.txt ") txt. write ("test") # open the file in r mode by default. Here, an IOError exception occurs, such as snapshot t AssertionError, e: print ("a <B") failed t TypeError, e: # Here e is the exception message print (e) handle T IOError, e: print (e) else: # execute the print ("Program execution successful") finally statement when no exception is found: # statements in the finally code block are often executed no matter whether they are common or not, which are usually used to open files, when an exception occurs during file processing, the file does not close the txt file. close ()