rnk
rnk

Reputation: 2204

Django - exception handling

I'm having this error page in Django.

Exception Type: IndexError
Exception Value: list index out of range
Exception Location: /home/nirmal/try/portfolio/views.py in vimeo_authorize, line 52

What I need is I just want to except this error in my views. I tried like this:

try:
       .........
except IndexError:
       .........

But it is not working. Could anyone give me the correct code?

Thanks!

Upvotes: 2

Views: 2788

Answers (2)

Chris Forrette
Chris Forrette

Reputation: 3214

A 'list index out of range' error is pretty basic in Python, as is exception handling, which you should reference the docs for:

In any case, a 'list index out of range' error means you're trying to get a specific index on an iterable that doesn't exist, for example this code:

mylist = [1,2,3,4]
print mylist[57]

... would throw that error, because only indexes 0 - 3 have been created and I am trying to access 57. To handle this exception, you would do something like:

try:
    item = mylist[57]
except IndexError:
    # Do some other stuff if we don't find the index we want...
    item = None

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799470

That code is correct. You're putting it in the wrong place.

Upvotes: 7

Related Questions