py-py’s blog

何か書くよ

エラーが起きたときの関数名を取得する

どこかでの関数でエラーが起きた場合に、その関数名を取得したいときに使う。

import inspect

def test_func():
    try:
        #  何かの処理
    except Exception as e:
        this_function_name = inspect.stack()[0][3]
        print(f"error happens {e} : {this_function_name}")