gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:38:58 24 4
gpt4 key购买 nike

以下是我的代码:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com