Ahmet DAL
Ahmet DAL

Reputation: 4683

Catching Any DoesNotExist Error

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?

Upvotes: 4

Views: 3959

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121226

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

from django.core.exceptions import ObjectDoesNotExist

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

Upvotes: 11

Related Questions