JavaのprintStackTrace()に相当するPythonの機能 [重複] 質問する

JavaのprintStackTrace()に相当するPythonの機能 [重複] 質問する

Pythonのexceptブロックでは、エラーメッセージを出力したいのですが、プログラムの実行を停止したくありません。次のようにする必要があることは理解しています。

try:
    1/0
except: 
    print errorMessage

exceptの部分には、Javaのようなものを入れようと考えていますprintStackTrace()

ベストアンサー1

を見てみましょうtraceback.print_exc()そして残りのtracebackモジュール。

import traceback

try:
    1/0
except:
    print '>>> traceback <<<'
    traceback.print_exc()
    print '>>> end of traceback <<<'

他にもいくつか例がありますtracebackドキュメントページの最後のほう

おすすめ記事