Catching Any DoesNotExist Error

南笙酒味 提交于 2019-12-24 01:48:32

问题


I am using Django 1.7. Normally you can catch DoesNotExist exception over your model like;

try:
   ...
except model.DoesNotExist, den:
   ...

I want to catch any DoesNotExist exception. I really don't want to care about its model. Actually, I really don't know which model DoesNotExist is passing through the code piece either. I mean, I am not able to know the model.

So I have to catch any DoesNotExist error somehow.

Is there a way to catch any DoesNotExist error in Django?


回答1:


DoesNotExist exceptions are subclasses of django.core.exceptions.ObjectDoesNotExist:

from django.core.exceptions import ObjectDoesNotExist

try:
    # ...
except ObjectDoesNotExist as den:
    # handle exception


来源:https://stackoverflow.com/questions/25928990/catching-any-doesnotexist-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!