TypeError:exceptions must be old-style classes or derived from BaseException, not str

2342G456DI8 picture 2342G456DI8 · Jul 16, 2012 · Viewed 67.3k times · Source

Following is my code:

test = 'abc'
if True:
    raise test + 'def'

And when i run this, it gives me the TypeError

TypeError: exceptions must be old-style classes or derived from BaseException, not str

So what kind of type should the test be?

Answer

user1393258 picture user1393258 · Jul 17, 2012

The sole argument to raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception).

Try this:

test = 'abc'
if True:
    raise Exception(test + 'def')