Any way to turn off exception handling? (debugging)

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


Duncan Booth wrote:

> Peter Otten <__peter__ at web.de> 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 instance_check.py
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
        try:
            1/0
        except ZeroDivisionError:
            print "caught"

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

Peter



More information about the Python-list mailing list