Python: 例外のエラーメッセージを取得する 質問する

Python: 例外のエラーメッセージを取得する 質問する

Python 2.6.6 では、例外のエラー メッセージをどのようにキャプチャできますか。

例えば:

response_dict = {} # contains info to response under a django view.
try:
    plan.save()
    response_dict.update({'plan_id': plan.id})
except IntegrityError, e: #contains my own custom exception raising with custom messages.
    response_dict.update({'error': e})
return HttpResponse(json.dumps(response_dict), mimetype="application/json")

これは機能しないようです。次のようになります:

IntegrityError('Conflicts are not allowed.',) is not JSON serializable

ベストアンサー1

まずそれを通過させますstr()

response_dict.update({'error': str(e)})

また、特定の例外クラスには、正確なエラーを返す特定の属性がある場合があることにも注意してください。

おすすめ記事