Ok, sorry for the noise but I've typed in the wrong test code. Here is the reals test case: class MyError(Exception): pass def test(x): d = {1:'1'} try: return d[x] except KeyError: raise MyError, 'not found' for i in range(10): try: y = test(i) except MyError, msg: print msg, i print 'DONE'