Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 541
Ahmet DAL Avatar asked Sep 16 '25 05:09

Ahmet DAL


1 Answers

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

from django.core.exceptions import ObjectDoesNotExist

try:
    # ...
except ObjectDoesNotExist as den:
    # handle exception
like image 50
Martijn Pieters Avatar answered Sep 17 '25 19:09

Martijn Pieters