Home >Backend Development >Python Tutorial >Python exception handling

Python exception handling

大家讲道理
大家讲道理Original
2017-05-28 10:06:011414browse

1. Exception basics:

In order to increase friendliness during the programming process, the error message is generally not displayed to the user when a bug occurs in the program. Instead, it displays a prompt page

Basic syntax:


1 try:2     pass3 except Exception as e:4 5     print(e)


Example: Addition of two numbers


 1 while True: 2     num1 = input('num1:') 3     num2 = input('num2:') 4     try: 5         num1 = int(num1) 6         num2 = int(num2) 7         result = num1 + num2 8     except Exception as e: 9         print ('出现异常,信息如下:')10         print (e)



2. Exception types

There are many types of exceptions in python , each exception is dedicated to handling a certain exception! ! !

Common exceptions:


 1 AttributeError 试图访问一个对象没有的属性,比如foo.x,但是foo没有属性x 2 IOError 输入/输出异常;基本上是无法打开文件 3 ImportError 无法引入模块或包;基本上是路径问题或名称错误 4 IndentationError 语法错误(的子类) ;代码没有正确对齐 5 IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5] 6 KeyError 试图访问字典里不存在的键 7 KeyboardInterrupt Ctrl+C被按下 8 NameError 使用一个还未被赋予对象的变量 9 SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)10 TypeError 传入对象类型与要求的不符合11 UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,12 导致你以为正在访问它13 ValueError 传入一个调用者不期望的值,即使值的类型是正确的


Example 1: IndexError


1 dic = ["haha", 'hehe']2 try:3     dic[10]4 except IndexError as e:5     print(e)     # list index out of range


Instance 2: ValueError


1 s1 = 'hello'2 try:3     int(s1)4 except ValueError as e:5     print(e)   # invalid literal for int() with base 10: 'hello'


For the above example, the exception class can only Used to handle specified exceptions. If exceptions are not specified, they cannot be handled.


1 s1 = 'hello'2 try:3     int(s1)4 except IndexError as e:5     print(e)   # 未捕获到异常,程序直接报错


At this time, you need to consider any exceptions that may occur in the program code


1 s1 = 'hello'2 try:3     int(s1)4 except IndexError as e:5     print(e)6 except KeyError as e:7     print(e)8 except ValueError as e:9     print(e)


But among the exceptions in python, there is a universal exception: Exception, which can catch any exception, that is:


1 s1 = 'hello'2 try:3     int(s1)4 except Exception as e:5     print(e)


There is this universal exception, but other exceptions cannot be ignored. Generally, exceptions for special processing or reminders need to be defined first, and finally Exception is defined to ensure the normal operation of the program


1 s1 = 'hello'2 try:3     int(s1)4 except KeyError as e:5     print( '键错误')6 except IndexError as e:7     print ('索引错误')8 except Exception, as e:9     print ('错误')


The above is the detailed content of Python exception handling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn