1

I have a query where I used exception:

try:
    sm = Something.objects.get(id=5)
except Something.DoesNotExist:
    pass

Here what does pass do ? I mean what if my whole view depends on sm and it does not get sm . Should I set "sm" manually like:

except Something.DoesNotExist:
    sm = anything

I just want to know what do pass do if it dont find whats queried. If it just ignore the part after except and go further then whats the point because my whole view depends on sm

Thank you

3 Answers 3

1

The keyword pass in the above context ignores DoesNotExist exception. If your program depends on the existence of sm somewhere down in your code, you should set it to None and check its value where you need it.

try:
    sm = Something.objects.get(id=5)
except Something.DoesNotExist:
    sm = None

if sm:
    ...

Alternatively, you can do your job in the else part of your try/catch block. It is executed only when the code in try block doesn't raise an exception.

try:
    sm = Something.objects.get(id=5)
except Something.DoesNotExist:
    # do something here..
else:
    print sm
    ...
Sign up to request clarification or add additional context in comments.

5 Comments

Also, if you just want to through a 404 exception if this Something isn't found, use this instead: something = get_object_or_404(Something, id=5)
can I set default value if the object is not found in except. like except Something.DoesNotExist: sm = "someone"
AFAIK, Django doesn't provide a shortcut method for this case but, of course you can do it in Python. However, if you have a default value already, I'd recommend you to set it somewhere above try/catch for the sake of code readability. You can write some method like get_object_or_default(Something, id=5, default="someone").
ohk then what about this like I want to use a property of sm like sm.phone_no if I dont get sm then how can I set default value for phone_no of sm
You can't use an instance (sm in this case) property unless instance is created. You should set the default value for phone number as a class property, maybe something like Something.DEFAULT_PHONE_NO, so you'll be able to use it even when sm isn't created.
0

pass is a null statement that does nothing, and is useful when you need a statement due to indentation rules, but don't want to write the code yet.

If you removed it to have the code:

try:
    sm = Something.objects.get(id=5)
except Something.DoesNotExist:

It would fail with a syntax error.


As for what to do if you don't find the item you are looking for, that very much depends on the rest of the code. However, in a case like this it would be better to show an error to someone as thats what has happened - you've requested an item that doesn't exist.

Comments

0

pass is the no-op and does nothing. It need to be in the except block to conform python syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.