pythontrycatch打印到⽇志_如何在Python中使⽤trycatch获取
更好的错误信息
Consider this try/catch block I use for checking error message stored in e.
Try/Catch to get the e
queryString = "SELECT * FROM benchmark WHERE NOC = 2"
try:
res = db.query(queryString)
except SQLiteError, e:
# `e` has the error info
print `e`
The e object here contains nothing more than the above string. When python reports an unhandled error, however, it shows a pretty detailed info as below:
Traceback (most recent call last):
File "fool.py", line 1, in
open("", "r")
IOError: [Errno 2] No such file or directory: ''
My question is, how can I get the information such as above (the file and the line number etc.)? Or, if e contains this info, how is it stored inside it?
解决⽅案
This will show the trace to the error.
import traceback
try:
res = db.query(queryString)
except SQLiteError, e:python的try和except用法
# `e` has the error info
print `e`
for tb in traceback.format__info()[2]):
print tb

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。