TypeError:exceptions必须是旧式类或从BaseException派生,而不是str [英] TypeError:exceptions must be old-style classes or derived from BaseException, not str

查看:670
本文介绍了TypeError:exceptions必须是旧式类或从BaseException派生,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码:

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

当我运行它时,它给了我TypeError

And when i run this, it gives me the TypeError

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

那么test应该是哪种类型?

推荐答案

raise的唯一参数表示要引发的异常.这必须是异常实例或异常类(从Exception派生的类).

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).

尝试一下:

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

这篇关于TypeError:exceptions必须是旧式类或从BaseException派生,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆