Common exception classification and processing method of Python

Source: Internet
Author: User

Python common exception types are roughly categorized as follows:

1.AssertionError: Exception thrown when assert assertion condition is false

2.AttributeError: Exception thrown when an object property that is accessed does not exist

3.IndexError: Exception thrown when a range of object indexes is exceeded

4.KeyError: Find an exception thrown by a nonexistent key in the dictionary

5.NameError: Exception thrown when accessing a non-existent variable

6.OSError: Exception generated by operating system

7.SyntaxError: This exception is thrown when a syntax error occurs

8.TypeError: This exception occurs because of a type error, usually an operation between types

9.ZeroDivisionError: This exception occurs when the divisor is 0 for mathematical operations

For more exceptions please refer to the official documentation:

2.7 Version Links

3.6 Version Links


Python Exception handling:

Example 1: The simplest way to handle an exception

#!/usr/bin/python#coding:utf8#try combined with except A = 1b = 2try:assert a > B #如果a >b judgment false will throw Assertionerror exception ex Cept assertionerror: #如果捕获到AssertionError异常将执行except下面的代码块 print ("A<b")

The above example output is a<b because A>b is false at the time of the assertion, the Assertionerror exception is thrown, and the statement in the except code block is executed when this exception is caught


Example 2: Catching exceptions using multiple except

#!/usr/bin/python#coding:utf8#try is used in conjunction with multiple except to execute sequentially in a try code block, as long as an exception is caught to stop execution A = 1b = 2c = "1" Try:assert a < b d = a + Cexcept assertionerror:print ("A<b") except Typeerror,e: #这里的 E for exception information print (e)

The above results are unsupported operand type (s) for +: ' int ' and ' str ' do not support integral type and string type addition, the preceding assertion is true, so there is no assertionerror exception, this time the following statement is executed, At this time, there is a TypeError exception, which will be executed except typeerror the following code block, followed by E for the exception of the error message, so the result here is to print out the error message of the exception


Example 3:try and the use of except and else

#!/usr/bin/python#coding:utf8a = 1b = 2c = "1" Try:assert a < b d = a+bexcept assertionerror,e:print ("A<b" ) except Typeerror,e:print (e) Else: #当try代码块中执行没有发现任何异常的时候执行这里的语句 print ("Program execution success Ful ")

The result of the above execution is


Example 4:try is used with except and else and finally (can have no else)

#!/usr/bin/python#coding:utf8#try is used in conjunction with multiple except to execute sequentially in a try code block, stopping execution whenever an exception is caught a = 1b = 2c =   "1" try:    assert a < b    d = a+b     txt = open ("/root/1.txt")     txt.write ("test")           #上面打开文件默认以r方式打开, this throws a IOError exception except assertionerror,e:     print ("A<b") except typeerror,e:       #这里的  e  For exception information     print (e) except ioerror,e:    print (e) else:                     # When no exception is found, execute the statement here     print ("program execution successful") finally:                  # The statements in the finally code block are often executed by people, usually in open files, in files processedProcess out of the abnormal exit, this time the file is not closed     txt.close () 

This article is from the "Blue _ Storm" blog, make sure to keep this source http://270142877.blog.51cto.com/12869137/1931771

Common exception classification and processing method of Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.