python异常捕捉处理

【python异常捕捉处理】
python异常捕捉处理

        • 一,try...except...捕捉异常

python异常处理相关需求,后续待补充。
一,try…except…捕捉异常
  1. 获取异常并定位出错误的代码是哪一行,通过traceback模块可定位出错代码的行数与错误信息
import traceback import timedef func(): try: assert 1 / 0 except Exception as e: print('func %s' % str(e))def func2(): try: assert 1 / 0 except Exception as e: error_detail = traceback.format_exc() print('func2 %s' % error_detail) print('*' * 50) time.sleep(0.1) traceback.print_exc()func() print('*' * 50) time.sleep(0.1) func2()

执行结果
func division by zero ************************************************** func2 Traceback (most recent call last): File "D:/Program Files/RPA-Project/Learning/test.py", line 14, in func2 assert 1 / 0 ZeroDivisionError: division by zero************************************************** Traceback (most recent call last): File "D:/Program Files/RPA-Project/Learning/test.py", line 14, in func2 assert 1 / 0 ZeroDivisionError: division by zero

    推荐阅读