python异常捕捉

#sys.exec_info()捕捉异常
import sys
import traceback
try:
    with open("hello.txt") as file:
        #没这个文件,肯定报错
        pass
except Exception as e:
    print("e-------",e)
    exc_type,exc_value,exc_tb=sys.exc_info()
    print("exc_type***",exc_type)
    print("exc_value***", exc_value)
    print("exc_tb***", exc_tb)
    print("exc_tb---",traceback.extract_tb(exc_tb))

result

e------- [Errno 2] No such file or directory: 'hello.txt'
exc_type*** <class 'FileNotFoundError'>
exc_value*** [Errno 2] No such file or directory: 'hello.txt'
exc_tb*** <traceback object at 0x0000018B37C81488>
exc_tb--- [<FrameSummary file C:/Users/Administrator.SC-201903160419/Desktop/note/projects/exception_catch.py, line 5 in <module>>]

 

posted @ 2020-03-11 22:28  腹肌猿  阅读(258)  评论(0编辑  收藏  举报