Any way to turn off exception handling? (debugging)

Peter Otten __peter__ at
Fri Feb 12 14:53:02 CET 2010

Duncan Booth wrote:

> Peter Otten <__peter__ at> wrote:
>> You could try to shadow the exception class with None:

> This works in Python 2.x but will break in Python 3. None is not a valid
> exception specification and Python 3 will check for that and complain.

> A better solution is to use an empty tuple as that is a valid exception
> specification so will work in both Python 2.x and 3.x:

Out of curiosity I tried a custom __subclasscheck__, but didn't get it to 
work in 3.1:

$ cat
class ZeroDivisionError(BaseException):
    class __metaclass__(type):
        def __subclasscheck__(self, other):
            print "performing subclass check --> %s" % catch
            return catch

if __name__ == "__main__":
    for catch in [True, False]:
        print "catch = %s" % catch
        except ZeroDivisionError:
            print "caught"

$ python2.6
catch = True
performing subclass check --> True
catch = False
performing subclass check --> False
Traceback (most recent call last):
  File "", line 11, in <module>
ZeroDivisionError: integer division or modulo by zero


More information about the Python-list mailing list