赞
踩
对于异常,大家都不陌生,也不怕大家笑话,我刚接触编程的时候我在控制台打印一个"hello world"都报了异常。
对于常见的错误,见下表:
异常 | 解释 |
---|---|
AttributeError | 访问的对象属性不存在 |
ImportError | 无法导入模块或者对象,主要是路径有误或名称错误 |
IndentationError | 代码没有正确对齐,主要是缩进错误 |
IndexError | 下标索引超出序列范围 |
IOError | 输入/输出异常,主要是无法打开文件 |
KeyError | 访问字典里不存在的键 |
NameError | 访问一个未声明的变量 |
OverflowError | 数值运算超出最大限制 |
SyntaxError | python语法错误 |
TabError | Tab和空格混用 |
TypeError | 不同类型数据之间的无效操作(传入对象类型与要求的不符合) |
ValueError | 传入无效的值,即使值的类型是正确的 |
ZeroDivisionError | 除法运算中除数0 或者 取模运算中模数为0 |
输出helloworld错误主要是因为使用的是中文状态下的双引号
- print("hello world!")
- print(“hello world!”)
-
-
这时候使用中文状态下双引号的print函数会报python语法错误,在进行编写代码的时候要注意使用的是英文状态下的字符。
print("hello world!")
SyntaxError: invalid character in identifier
对于ZeroDivisionError,常见于计算中,如:
- a = 1/0
- print(a)
在数学计算中,0是不能作为被除数参与运算的
ZeroDivisionError: division by zero
- x = 520 + "Messi"
- print(x)
对于数字类型和字符类型的数据是不能加在一起的
TypeError: unsupported operand type(s) for +: 'int' and 'str'
对于异常,大家都不想遇见,如何在程序出错之前就找到异常,在代码中捕获并且处理异常呢,这里我们用到try - except语句。
当try中的代码块没有出错时,会继续执行try中的代码,在出现异常的时候,会执行except中的代码。
- try:
- 1/1 #检测范围
- print("程序正常")
- except: #[expression[as identifer]]
- print("出错了。。") #异常处理代码
-
- try:
- 1/0
- print("程序正常")
- except:
- print("出错了。。")
程序正常
出错了。。
在except后面指定异常的时候,如果指定的异常和try代码块中的异常不一样的话,不会执行except中的代码,会继续报错。
- try:
- 1/0 #异常和except中定义的一样
- print("程序正常")
- except ZeroDivisionError as e:
- print("出错了。。",e)
-
-
- try:
- 1/0 #异常和except中定义的不一样
- print("程序正常")
- except TypeError as e:
- print("出错了。。",e)
-
-
- #可以通过as e 打印出来具体异常
出错了。。 division by zero
ZeroDivisionError: division by zero
可以再expect后面写入多个异常类型,当try代码块中的异常属于其中一个的时候,就会直接执行except中的内容。
- #写法一
- try:
- 1/0
- 520 +"messi"
- except (ZeroDivisionError,TypeError,SyntaxError):
- print("出错了。。")
-
- #写法二
- try:
- 1/0
- 520 +"messi"
- except ZeroDivisionError:
- print("ZeroDivisionError")
- except SyntaxError:
- print("SyntaxError")
- except TypeError:
- print("TypeError")

出错了。。
ZeroDivisionError
我们发现写法一中的两个异常都被捕获了,而写法二中的异常在捕获了ZeroDivisionError的异常后就直接执行except中的代码,而忽略了对 520 +"messi" 异常的执行。
当我们的代码没有问题的时候,就会执行else中的代码
- #例1
- try:
- 1/1
- except :
- print("出错了。。")
-
- else:
- print("没出错!!")
-
- #例2
- try:
- 1/1
- except :
- print("出错了。。")
-
- else:
- print("没出错!!")

没出错!!
出错了。。
无论代码有没有出现错误,都会执行finally中的代码,常用于放置一个程序必须要运行的代码,列如文件的关闭,资源的释放等。
- try:
- 1/1
- except :
- print("出错了。。")
-
- else:
- print("没出错!!")
- finally:
- print("有没有错都执行我!!!")
-
- try:
- 1/0
- except :
- print("出错了。。")
-
- else:
- print("没出错!!")
- finally:
- print("有没有错都执行我!!!")

运行结果为:
没出错!!
有没有错都执行我!!!
出错了。。
有没有错都执行我!!!
- #先内部异常,然后外部异常
- try:
- try:
- 520 + "Messi"
- except:
- print("内部异常!")
- 1/0
- except:
- print("外部异常!")
- finally:
- print("结束")
-
-
- #先外部异常,然后内部异常
- try:
- 1/0
- try:
- 520 + "Messi"
- except:
- print("内部异常!")
- except:
- print("外部异常!")
- finally:
- print("结束")

当先外部异常的时候,不会执行内部代码,当先内部异常的时候,会执行外部代码。
内部异常!
外部异常!
结束
外部异常!
结束
可以触发异常,用于直接抛出Python内置异常,或用户自定义的异常。
- #定义已有异常的提示
- raise ZeroDivisionError("除数不能为0")
-
- try:
- 1/0
- except:
- raise ZeroDivisionError("不能这样写啊!")
-
-
- #自定义异常
- raise ZiDingYi("我是自定义异常!")
-
-
- #异常链,表明异常来自于那里
- raise ValueError("除数不能为0") from ZeroDivisionError
-

>>>Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
raise ZeroDivisionError("除数不能为0")
ZeroDivisionError: 除数不能为0
>>> raise ZiDingYi("我是自定义异常!")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
raise ZiDingYi("我是自定义异常!")
NameError: name 'ZiDingYi' is not defined
>>> try:
1/0
except:
raise ZeroDivisionError("不能这样写啊!")Traceback (most recent call last):
File "<pyshell#3>", line 2, in <module>
1/0
ZeroDivisionError: division by zeroDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#3>", line 4, in <module>
raise ZeroDivisionError("不能这样写啊!")
ZeroDivisionError: 不能这样写啊!
>>> raise ValueError("除数不能为0") from ZeroDivisionError
ZeroDivisionErrorThe above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
raise ValueError("除数不能为0") from ZeroDivisionError
ValueError: 除数不能为0
assert只能引发AssertionError异常,通常用于判断,类似于if语句。
- a = "Messi"
- assert a == "Messi"
- assert a != "Messi"
当结果为true时什么都不会发生,当结果时false时,会报出错误。
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
assert a != "Messi"
AssertionError
以上内容就是异常的相关知识了,欢迎补充!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。