[Python] "Error exception must derive from BaseException" when inheriting from BaseException...

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    What's wrong with the following code (under Python 2.7.1):

    class TestFailed(BaseException):
    def __new__(self, m):
    self.message = m

    def __str__(self):
    return self.message

    try:
    raise TestFailed('Oops')
    except TestFailed as x:
    print x


    When I run it, I get:

    Traceback (most recent call last):
    File "x.py", line 9, in <module>
    raise TestFailed('Oops')
    TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType


    But it looks to me that TestFailed does derive from BaseException.

    Continue reading...

Compartilhe esta Página