python - TypeError:exceptions 必须是旧式类或派生自 BaseException,而不是 str

标签 python typeerror raise

以下是我的代码:

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/

相关文章:

python - 需要 Pandas 中的行值列表

python - Scipy.integrate浮点错误

delphi - 如何禁用 Delphi 中的所有异常引发?

python - 用 matplotlib 绘图 : TypeError: float() argument must be a string or a number

python - Django/Python 不可排序类型 Story() < Story() with sorting function

wpf - 如何在形状上实现拖动增量

c - while 循环中的 raise()

python - “tee”不被识别为内部或外部命令、可操作程序或批处理文件

python - 如何将默认值传递给 Altair 编码

python - 通过避免循环在 pandas 中向量化代码