首页 / PYTHON / Python学习笔记:异常处理
Python学习笔记:异常处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python学习笔记:异常处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3458字,纯文字阅读大概需要5分钟。
内容图文
![Python学习笔记:异常处理](/upload/InfoBanner/zyjiaocheng/601/d1c2cfdc40e64ad98ec4224343f73b67.jpg)
Python学习笔记:异常处理
基本的异常处理
python与其它语言一样,也是支持异常处理的,我们看一个例子:
n=1/0
输出
File "d:\workspace\python\set.py", line 1, in
n=1/0
ZeroDivisionError: division by zero
可以看到,除零错误以一个ZeroDivisionError形式抛出。
这里和其它语言中一样,可以用try/catch语句来捕获异常,稍有区别的是这里写作try/except,但效果相同:
try:
n=1/0
except:
print("here is a catched error")
输出
here is a catched error
这里没有指定捕获的异常类型,类似在PHP中指定基类Exception,可以捕获到所有类型,当然我们也可以捕获指定异常:
try:
n = 1/0
except ZeroDivisionError:
print("here is a catched ZeroDivisionError")
输出
here is a catched ZeroDivisionError
如果需要捕获多种异常,然后分情况处理的,这样写:
try:
n = 1/0
except ZeroDivisionError:
print("here is a catched ZeroDivisionError")
except Exception:
print("here is a catched Exception")
输出
here is a catched ZeroDivisionError
此外python同样支持finaly:
def testTryCatch(a):
try:
n = 1/a
except ZeroDivisionError:
print("here is a catched ZeroDivisionError")
except Exception:
print("here is a catched Exception")
finally:
print("whatever error will happen here is will called")
testTryCatch(0)
testTryCatch(1)
输出
here is a catched ZeroDivisionError
whatever error will happen here is will called
whatever error will happen here is will called
无论是否捕获到异常,finally后的语句都会输出。
异常继承关系
python的异常继承关系与PHP略有不同,下面简单罗列一些python的继承关系:
- BaseException
- SystemExit
- KeyboardInterrupt
- GeneratorExit
- Exception
- ArithmeticError
- ZeroDivisionError
可以看出在Python中,最基础的异常类是BaseException,它是所有异常的基类,可以用于捕获各种类型的异常。
更多的异常继承关系请阅读简明教程。
抛出异常
当然,Python同样支持抛出异常,但并不是throw
,而是使用raise
。
对于Python并未采用其它语言的一些关键字我也是颇为费解,但对我等码农来说,学就完事了。
def testTryCatch(a):
try:
n = 1/a
except ZeroDivisionError as error:
print("I don't want deal ZeroDivisionError")
raise error
try:
testTryCatch(0)
except BaseException as error:
print(type(error))
输出
I don't want deal ZeroDivisionError
<class 'ZeroDivisionError'>
在上边的例子中,testTryCatch
程序中捕获了异常,但没有处理,直接抛出,到外部主程序中才进一步处理。此外可以看到,我们用句柄BaseException来捕获异常,但异常类型并未改变。
自定义异常
除了处理程序运行当中编译器抛出的异常,开发者有时候也会主动定义一些异常并抛出,而且这相当有用,尤其是在编写一些网络服务时,用异常来实现错误处理会让程序相当简洁。
class customError(Exception):
def __init__(self):
pass
def __str__(self):
return "this is a custom error"
try:
error=customError()
raise error
except Exception as e:
print(str(e))
输出
this is a custom error
上面的例子中,定义了一个用户自定义异常customError
,可以看到和类定义完全没什么两样,并且在异常中重写了__str__
便于输出自定义信息,如果在网络开发中,可以进一步定义相应的错误码和错误提示信息以方便客户端接收。
日志
除了异常,Python还提供一个用于错误排查的模块——日志:
import logging
logging.basicConfig(filename='main.log',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(message)s')
logging.error("this is error message1")
logging.error("this is error message2")
main.log
2021-03-05 17:16:34,829 - set.py[line:4] - this is error message1
2021-03-05 17:16:34,831 - set.py[line:5] - this is error message2
使用logging.basicConfig()
对logging模块设置后,就可以很容易地记录程序错误信息了,至少比用户自行定义日志记录功能要方便很多。
调试
简明教程介绍了一种Python的手工调试工具Pdp,但老实说,VSCode的调试功能已经相当强大了,所以这里不想多做赘述,想了解Pdp调试的请移步简明教程。
内容总结
以上是互联网集市为您收集整理的Python学习笔记:异常处理全部内容,希望文章能够帮你解决Python学习笔记:异常处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。