except Exception as e:#这里的e其实是类Exception的对象,这句话的意思是如果try语句块中出现异常,就创建一个异常的对象e,对象e中封装了所有的异常信息。
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置。下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序结构。如下所示
复制代码
try:
...
except Exception, e:
...
复制代码
1、str(e)
返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常信息
'integer division or modulo by zero'
2、repr(e)
给出较全的异常信息,包括异常信息的类型,如1/0的异常信息
"ZeroDivisionError('integer division or modulo by zero',)"
3、e.message
获得的信息同str(e)
4、采用traceback模块
需要导入traceback模块,此时获取的信息最全,与python命令行运行程序出现错误信息一致。使用traceback.print_exc()打印异常信息到标准错误,就像没有获取一样,或者使用traceback.format_exc()将同样的输出获取为字符串。你可以向这些函数传递各种各样的参数来限制输出,或者重新打印到像文件类型的对象。
示例如下:
复制代码
import traceback
print '########################################################'
print "1/0 Exception Info"
print '---------------------------------------------------------'
try:
1/0
except Exception, e:
print 'str(Exception):\t', str(Exception)
print 'str(e):\t\t', str(e)
print 'repr(e):\t', repr(e)
print 'e.message:\t', e.message
print 'traceback.print_exc():'; traceback.print_exc()
print 'traceback.format_exc():\n%s' % traceback.format_exc()
print '########################################################'
print '\n########################################################'
print "i = int('a') Exception Info"
print '---------------------------------------------------------'
try:
i = int('a')
except Exception, e:
print 'str(Exception):\t', str(Exception)
print 'str(e):\t\t', str(e)
print 'repr(e):\t', repr(e)
print 'e.message:\t', e.message
print 'traceback.print_exc():'; traceback.print_exc()
print 'traceback.format_exc():\n%s' % traceback.format_exc()
print '########################################################'
复制代码
示例结果
补充 1(更新于2020.8.1)
对于 Python 3 的 Exception,与 Python 2 的 Exception 相比,有两个需要注意的地方:
1)在 Python 3 Exception 的 except 子句中,不支持使用逗号 ',' 分隔 Exception 和 e,所以需要采用 as 关键词进行替换;
2)与 Python 2 Exception 类相比,Python 3 Exception 类没有 message 成员变量。针对这个问题,可以采用 sys.exc_info() 方法获取得到相关的异常信息。以 1/0 异常处理为例,更新的程序如下:
复制代码
import sys
import traceback
print('########################################################')
print("1/0 Exception Info")
print('---------------------------------------------------------')
try:
1/0
except Exception as e:
print('str(Exception):\t', str(Exception))
print('str(e):\t\t', str(e))
print('repr(e):\t', repr(e))
# Get information about the exception that is currently being handled
exc_type, exc_value, exc_traceback = sys.exc_info()
print('e.message:\t', exc_value)
print("Note, object e and exc of Class %s is %s the same." %
(type(exc_value), ('not', '')[exc_value is e]))
print('traceback.print_exc(): ', traceback.print_exc())
print('traceback.format_exc():\n%s' % traceback.format_exc())
print('########################################################')
复制代码
注:
1) sys.exc_info() 方法可以获取正在处理的异常信息,即 except 子句正在处理的异常,其返回值为一个tuple类型的三元组(exc_type, exc_value, exc_traceback),其中,exc_type为获取到的异常类型;exc_value为该异常类型对象;exc_traceback为一个 traceback 对象,包含异常最初发生的调用栈信息。
2) as 关键字以及 sys.exc_info() 方法对于 Python 2 同样适用。
3) 程序中的变量 e 和 exc_value 是同一个异常类型实例对象。
在使用Python3做自动化测试过程中可能会遇到,assert函数不加try except,就可以正常在报告里体现用例不通过,加上变成通过。
这是因为在使用try except 时,捕获了assert函数产生的AssertionError异常,导致异常没有上抛,这时只需要在后面加上 raise 就可以再次把它抛出。
try:# 找到输入框并输入测试数据
self.driver.find_element_by_id("kw").send_keys(testdata)
# 找到搜索按钮,并单击
self.driver.find_element_by_id("su").click()
time.sleep(3)
# 断言期望结果中是否出现页面源代码中
self.assertIn(expectdata,self.driver.page_source)
time.sleep(3)
except AssertionError as e:
logger.info(u"搜索\"%s\",期望\"%s\"失败" % (testdata, expectdata))
raise
else:
logger.info(u"搜索\"%s\",期望\"%s\"通过"%(testdata,expectdata))