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')
You can't raise
a str
. Only Exception
s can be raise
d.
So, you're better off constructing an exception with that string and raising that. For example, you could do:
test = 'abc'
if True:
raise Exception(test + 'def')
OR
test = 'abc'
if True:
raise ValueError(test + 'def')
Hope that helps
It should be an exception.
You want to do something like:
raise RuntimeError(test + 'def')
In Python 2.5 and below, your code would work, as then it was allowed to raise strings as exceptions. This was a very bad decision, and so removed in 2.6.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With