Skip to content Skip to sidebar Skip to footer

Exceptions Must Derive From BaseException

What am I missing here? import sys class MyBaseError(BaseException): def __init__(self, message, base_message=None, *args): self.message = message sel

Solution 1:

In your sayonara() function, it seems you are attempting to raise a tuple of exceptions. The problem is that sys.exc_info()[2] is a traceback, and not an exception, which is the cause of your break. I verified this by placing the following line at the top of the exception block:

print(type(sys.exc_info()[2]))

I'm not certain of what you are trying to do for sure, but a working version of sayonara() is as follows:

def sayonara():
    try:
        run_me()
    except (MyBaseError) as e:
        raise MyBaseError("unable to run", e, e.args)

If you want to include the traceback, you'll need to update your custom Error classes to handle that argument being passed through.


Solution 2:

That means you should raise the Exception class or an instance of it. For example

try:
   1 + "String"
except TypeError:
   raise TypeError("unsupported operand type(s) for +: 'int' and 'str'")

Post a Comment for "Exceptions Must Derive From BaseException"