import sys def check(obj, cls=None): if cls is None: cls = type(obj) try: raise obj except str(obj): print "caught by identity as:", cls, obj except cls, obj: print "caught as:", cls, obj except: print "not caught" else: print "no exception" print "exc_info %r" % (sys.exc_info(), ) print "exc_type %r" % (sys.exc_type, ) print class newint(int): pass class newstr(str): pass check(42) check(newint(43)) check(newint(43), int) check(44L) check(45.) check(46j) check(object()) check(()) check((47,)) check([]) check({}) check("foo") check(newstr("foo")) check(newstr("foo"), str) check(u"bar") check(int) check(type)