python输出try异常的相关信息

不使用logging模块,只是简单的输出某一个函数的异常信息

# coding:utf-8
import sys

import pandas as pd


def test():
    try:
        pd.DataFrame('')
    except ValueError:
        error_msg = '执行异常![{}]-[{}:{}]-[{} {}]'.format(
            sys.exc_info()[2].tb_frame.f_code.co_filename,
            sys.exc_info()[2].tb_frame.f_code.co_name,
            sys.exc_info()[2].tb_lineno,
            sys.exc_info()[0],
            sys.exc_info()[1]
        )
        print(error_msg)


test()

输出信息如下:

执行异常![E:\7、Projects\1、PythonProjects\测试.py]-[test:9]-[<class 'ValueError'> DataFrame constructor not properly called!]
posted @ 2023-12-27 10:54  cnblogs用户  阅读(26)  评论(0编辑  收藏  举报