TypeError:exception必须是旧式的类或派生自BaseException,而不是str
以下是我的代码:
test = 'abc' if True: raise test + 'def'  而当我运行这个,它给了我TypeError 
 TypeError: exceptions must be old-style classes or derived from BaseException, not str 
 那么test应该是什么types? 
提出的唯一论点表明了要提出的例外。 这必须是exception实例或exception类(从Exception中派生的类)。
尝试这个:
 test = 'abc' if True: raise Exception(test + 'def') 
 你不能raise 。 只有Exception s可以raise d。 
所以,最好用这个string构造一个exception并提高它。 例如,你可以这样做:
 test = 'abc' if True: raise Exception(test + 'def') 
要么
 test = 'abc' if True: raise ValueError(test + 'def') 
希望有所帮助
这应该是一个例外。
你想要做的事情如:
 raise RuntimeError(test + 'def') 
在Python 2.5和更低版本中,你的代码可以工作,因为它允许将string作为例外来引发。 这是一个非常糟糕的决定,在2.6中删除了。