Python Sys获取异常行号

问题描述 投票:0回答:1

traceback.format_exc()我可以看到异常发生的真实线路,这是line 6

....
  File "main.py", line 6, in testDef
    raise ValueError('Value error, exit!')
ValueError: Value error, exit!

与qazxsw poi我怎么能得到这个数字,目前它返回qazxsw poi而不是qazxsw poi

sys

产量

line 16
python
1个回答
0
投票

我找到了答案,我必须循环通过6

import concurrent.futures, traceback, sys
from concurrent.futures import ThreadPoolExecutor

def testDef(arg):
  if arg == 'b':
    raise ValueError('Value error, exit!')

args = ['a', 'b']
pool = ThreadPoolExecutor(2)
fs = []
for a in args:
  fs.append(pool.submit(testDef, a))
concurrent.futures.wait(fs)
for fut in fs:
    try:
      fut.result()
    except Exception as e:
      #print(traceback.format_exc())
      exc_type, exc_obj, tb = sys.exc_info()
      lineno = tb.tb_lineno # ?
      filename = tb.tb_frame.f_code.co_filename
      print('Error File "%s", line %s: %s' % (filename,lineno, e))
© www.soinside.com 2019 - 2024. All rights reserved.