Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I don't know where I went wrong in the below code (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 execute 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

According to me, it looks like TestFailed is derived from BaseException

1 Answer

0 votes
by (26.4k points)

There's a static method which will return an instance. That's __new__ method. Instead of that, you can use __init__ method

Code:

class TestFailed(Exception):

    def __init__(self, m):

        self.message = m

    def __str__(self):

        return self.message

try:

    raise TestFailed('Oops')

except TestFailed as x:

    print x

Looking for python tutorial? Join python training course and gain more knowledge on the concepts of python.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...