Except block not catching exception in ipython notebook

lib

When I try this simple example in my current Python environment (a ipython notebook cell) I am not able to catch TypeError exception:

a = (2,3)
try:
  a[0] = 0
except TypeError:
  print "catched expected error"
except Exception as ex:
  print type(ex), ex

I get:

<type 'exceptions.TypeError'> 'tuple' object does not support item assignment

When I try to run the same copy-pasted code in a different ipython notebook on the same computer I get the expected output: catched expected error.

I understand it has something to do with my current environment, but I have no idea where to start looking! I tried also another example with AttributeError and in that case the catch block works.

EDIT: When I tried:

   >>> print AttributeError
   <type 'exceptions.AttributeError'>
   >>> print TypeError
   <type 'exceptions.AttributeError'>

I remembered that earlier in the session I made an error, which renamed TypeError:

try:
    group.apply(np.round, axis=1) #group is a pandas group
except  AttributeError, TypeError : 
#it should have been except  (AttributeError, TypeError)
    print ex

which gave me:

 ('rint', u'occurred at index 54812')
Andy Feely

I think it may be that the TypeError has to be implicitly imported for some environments:

from exceptions import TypeError

Give that a go!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Python, how to drop into the debugger in an except block and have access to the exception instance?

Mocking a function to raise an Exception to test an except block

Catching multiple exception types in one catch block

Exception gets executed twice and it gets caught by a different except block

try-except block: analogue for 'else' if exception was raised

Always perform finally block except for one exception

Python: Propagate an exception through a Try/Except Block with multiple Excepts

Why catch block of base class is catching the exception when I am throwing object of derived class?

try except not catching the exception

How to continue a loop after catching exception in try ... except

How to remove duplicate exception error in python try/except block?

Exception not being handled by the try-except block in Python

Python: raise another exception in except block to catch later

Catching inner exception in F# async block

How do I move back to try block after catching exception

Executing a code block even after catching an exception in Scala

@try@catch block not catching inside exception

Django exception handler for the default-brench in try-except block

Exception not catching in try/catch block

Nested Try-Catch Block Not Catching Exception

Catching an Exception with try-catch inside an if/switch block

How to test the exception of try/except block using pytest

Why exception isn't printed in except block

W0703: Catching too general exception Exception (broad-except)

Exception is thrown even though there is a try...except block

Python: Except block capturing exception that is not listed for block

Try except not catching FileNotFoundError

Handle multiple exception in Python with only one try - except block

After catching an error but except statement not resolving, is it good standard/makes sense to throw an exception?