问题
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