9

For some reason my code is not catching an exception when I throw it. I have

def trim_rad(rad): ... if not modrad.shape[0]: raise IndexError("Couldn't find main chunk") return modrad, thetas 

Then later I call that function:

try: modrad, thetas = trim_rad(rad) except IndexError("Couldn't find main chunk"): return 0 

Yet I still get a traceback with that exception. What am I doing wrong?

7
  • 5
    You're only supposed to put the type name there, not an instance of the type. If you want to further narrow it down to handle only a special IndexError that you will be throwing and leave regular ole IndexErrors alone, create a narrower exception type that inherits from IndexError and raise/catch that. Commented Aug 15, 2013 at 18:20
  • 1
    I'm surprised this doesn't TypeError. I thought I tried except 1: before and got a TypeError, but trying it now, it just passes through. Commented Aug 15, 2013 at 18:23
  • @user2357112 Evidently it does cause a TypeError in Python 3: ideone.com/yy4XAy. You're right though, it doesn't matter in 2.x Commented Aug 15, 2013 at 18:28
  • @Asad It's weird that there wasn't any warning that I was doing it wrong. So if I want to catch that specific error and not other IndexErrors, is it more standard to subclass IndexError, or parse the message and re-raise the error if it's not the specified message? I only use the error in this instance, would making a new subclass be a bit too heavy duty? Commented Aug 15, 2013 at 19:48
  • 1
    @ari It is better to subclass IndexErrors (IMO at least), but it is entirely up to you. Both approaches would work, although using string comparison for crucial decision making and control flow always feels gross to me. I'm no Python expert, so YMMV. Commented Aug 15, 2013 at 19:52

4 Answers 4

14

Catch just the IndexError.

try: raise IndexError('abc') except IndexError('abc'): print 'a' Traceback (most recent call last): File "<pyshell#22>", line 2, in <module> raise IndexError('abc') IndexError: abc try: raise IndexError('abc') except IndexError: print 'a' a # Output 

So, reduce your code to

try: modrad, thetas = trim_rad(rad) except IndexError: return 0 

If you want to catch the error message too, use the following syntax:

try: raise IndexError('abc') except IndexError as err: print err abc 
Sign up to request clarification or add additional context in comments.

Comments

11

You gave except an instance of an IndexError. Do this instead:

try: modrad, thetas = trim_rad(rad) except IndexError: print "Couldn't find main chunk" return 0 

Here is an example:

>>> try: ... [1][1] ... except IndexError('no'): ... pass ... Traceback (most recent call last): File "<stdin>", line 2, in <module> IndexError: list index out of range >>> try: ... [1][1] ... except IndexError: ... pass ... >>> 

Comments

3

change

except IndexError("Couldn't find main chunk"): 

to

except IndexError: 

Comments

3

You seem to be catching the exception wrong. You catch exceptions of type, the notation below will assign the exception to e so you can read the description in your except handler.

try: modrad, thetas = trim_rad(rad) except IndexError as e: print e.message return 0 

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.