Python exception handling - line number

前端 未结 6 1264
感动是毒
感动是毒 2020-12-12 12:24

I\'m using python to evaluate some measured data. Because of many possible results it is difficult to handle or possible combinations. Sometimes an error happens during the

6条回答
  •  醉酒成梦
    2020-12-12 13:27

    I always use this snippet

    import sys, os
    
    try:
        raise NotImplementedError("No error")
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
    

    for different views and possible issues you can refer When I catch an exception, how do I get the type, file, and line number?

提交回复
热议问题