print
和traceback
混乱,即使print
之前应该发生,traceback
打印在上方,中间和下方。
try:
zerrr()
except Exception as e:
error_json, raise_error, throw = excHandler(e, throw=False)
# print(type(k))
if throw:
raise Exception(raise_error)
error_dict = json.loads(error_json)
print(error_dict)
try:
typerr()
except Exception as e:
error_json, raise_error, throw = excHandler(e, throw=True)
# print(type(k))
if throw:
# raise Exception(raise_error)
traceback.print_exc()
error_dict = json.loads(error_json)
# print(error_dict)
finally:
# Necessary calls
musthappen()
首次运行输出:
Traceback (most recent call last):
{'1515142630.125066': {'Line': 48, 'Error_call': "'return 1/0'", 'File': 'C:/Users/ankit.chouksey/Mariott/Pys/exclog.py', 'Error': ['ZeroDivisionError: division by zero\n'], 'Function': 'zerrr'}}
File "C:/Users/ankit.chouksey/Mariott/Pys/exclog.py", line 69, in <module>
This is always happening irrespective of exceptions
typerr()
File "C:/Users/ankit.chouksey/Mariott/Pys/exclog.py", line 51, in typerr
return 1+'a'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
首次运行:
在追溯之前字典是打印的,但它正在里面打印。
类似地,声明“这总是发生而不管异常”是在追溯之后打印,但它在两者之间打印。
第二次运行输出:
Traceback (most recent call last):
{'1515143476.309911': {'Line': 48, 'Error': ['ZeroDivisionError: division by zero\n'], 'Function': 'zerrr', 'Error_call': "'return 1/0'", 'File': 'C:/Users/ankit.chouksey/Mariott/Pys/exclog.py'}}
This is always happening irrespective of exceptions
File "C:/Users/ankit.chouksey/Mariott/Pys/exclog.py", line 69, in <module>
typerr()
File "C:/Users/ankit.chouksey/Mariott/Pys/exclog.py", line 51, in typerr
return 1+'a'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
第二次运行 两个印刷品都在一起,但内部追溯。
答案 0 :(得分:0)
我在计算机上测试了类似的代码
在terminal
中测试时,输出始终是正常的并且是预期的
然而,当在IDE(PyCharm)
中进行测试时,输出就像你所说的那样混乱
您是否正在IDE
测试代码?
我认为答案可能取决于您正在使用的IDE。(IDE可能会在多线程/多处理中将消息推送到stdout
?(不确定))