包含编程资料、学习路线图、源代码、软件安装包等!【点击这里】领取!!!
一般情况,Python 无法正常处理程序时,就是会产生异常造成程序崩溃,举个例子:
x = 3/0
程序崩溃/报错:ZeroDivisionError: division by zero
# 读取123.txt文件,实际上该文件并不存在
fp =open("123.txt","r")
程序崩溃/报错:FileNotFoundError: [Errno 2] No such file or directory: '123.txt'
对于以上的各种异常情况,我们可以通过 try…except…解决,那么具体有哪些异常能捕获呢?如下所示
try:
<代码>
except:
print("异常说明")
示例代码:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
x = 3 / 0 # 程序出现异常,不会继续执行后面的代码
print("hello world")
except:
print("异常说明:分母不能为0")
print("程序正常结束")
'''
输出结果:
异常说明:分母不能为0
程序正常结束
'''
try:
<代码>
except:
print("异常说明")
示例代码:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
x = 3 / 0 # 程序出现异常,不会继续执行后面的代码
print("hello world")
except:
print("异常说明:分母不能为0")
print("程序正常结束")
'''
输出结果:
异常说明:分母不能为0
程序正常结束
'''
try:
< 语句 >
except < 异常名1 >:
print('异常说明1')
except < 异常名2 >:
print('异常说明2')
except < 异常名3 >:
print('异常说明3')
该种异常处理语法的规则是:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
fp = open("123.txt","r") # 程序出现异常,不会继续执行后面的代码
print("hello world")
fp.close()
except GeneratorExit:
print("异常说明:GeneratorExit")
except FloatingPointError:
print("异常说明:FloatingPointError")
except OverflowError:
print("异常说明:OverflowError")
except ZeroDivisionError:
print("异常说明:ZeroDivisionError")
except IOError:
print("异常说明:IOError")
print("程序正常结束")
'''
输出结果:
异常说明:IOError
程序正常结束
'''
try:
< 语句 >
except Exception as e: # 自动识别异常信息,并将异常信息保存在e中
print('异常说明:',e)
示例代码:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
fp = open("python.txt","r") # 程序出现异常,不会继续执行后面的代码
print("hello world")
fp.close()
except Exception as e:
print("异常说明:",e)
print("程序正常结束")
'''
输出结果:
异常说明: [Errno 2] No such file or directory: 'python.txt'
程序正常结束
'''
如果判断完没有异常之后还想做其他事,就可以使用下面这样的 else 语句,注意:是在 try 中没有异常情况下,才会执行 else 之后的代码。
try:
< 语句 >
except < 异常名1 >:
print('异常说明1')
except < 异常名2 >:
print('异常说明2')
else:
< 语句 > # try语句中没有异常则执行此段代码
示例代码:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
print("hello world")
except GeneratorExit:
print("异常说明:GeneratorExit")
except FloatingPointError:
print("异常说明:FloatingPointError")
except OverflowError:
print("异常说明:OverflowError")
except ZeroDivisionError:
print("异常说明:ZeroDivisionError")
else:
print("i love you")
print("程序正常结束")
'''
输出结果:
hello world
i love you
程序正常结束
'''
try…finally…语句无论是否发生异常都将会执行最后的代码。
try:
< 语句 >
finally:
< 语句 >
示例代码:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
fp = open("python.txt","r") # 程序出现异常,不会继续执行后面的代码
print("hello world")
fp.close()
except Exception as e:
print("异常说明:",e)
finally:
print("finally") # 无论是否发生异常都将会执行最后的代码
print("程序正常结束")
'''
输出结果:
raise Exception!
异常说明: [Errno 2] No such file or directory: 'python.txt'
finally
程序正常结束
'''
可以使用 raise 语句自己触发异常,语法如下:
raise [Exception [, args [, traceback]]]
语句中 Exception 是异常的类型(例如,IOError )参数标准异常中任一种,args 是自已提供的异常参数。最后一个参数是可选的(在实践中很少使用),如果存在,是跟踪异常对象;示例代码如下:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:快乐的小绵羊
@Blog(个人博客地址): www.codersrc.com
@File:Python 异常.py
@Time:2021/04/22 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
try:
raise Exception("raise Exception!") # 触发异常后,后面的代码就不会再执行
print("hello world")
except Exception as e:
print(e)
finally:
print("OK")
'''
输出结果:
raise Exception!
OK
'''
在 Python 开发中,为了增加程序的健壮性,异常处理 try…except… 是必须掌握的内容.
包含编程资料、学习路线图、源代码、软件安装包等【点击领取】领取!!!