以下是我的代码:
test = 'abc'
if True:
raise test + 'def'
当我运行它时,它给了我 TypeError
TypeError: exceptions must be old-style classes or derived from BaseException, not str
那么test
应该是什么样的类型呢?
最佳答案
raise 的唯一参数表示要引发的异常。这必须是异常实例或异常类(派生自 Exception 的类)。
试试这个:
test = 'abc'
if True:
raise Exception(test + 'def')
关于python - TypeError:exceptions 必须是旧式类或派生自 BaseException,而不是 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497234/