我得到“TypeError:异常必须从BaseException派生”,即使我确实定义了它

时间:2013-07-23 19:29:33

标签: python exception

根据python文档,Exception派生自BaseExceptions,我应该将它用于用户定义的异常。所以我有:

class VisaIOError(Exception):

    def __init__(self, error_code):
        abbreviation, description = _completion_and_error_messages[error_code]
        Error.__init__(self, abbreviation + ": " + description)
        self.error_code = error_code

 raise(visa_exceptions.VisaIOError, status)

但是我得到了(trackback代码段):

   File "C:\Python32\Lib\site-packages\pyvisa\vpp43.py", line 400, in check_status

    raise(visa_exceptions.VisaIOError, status)
    TypeError: exceptions must derive from BaseException

注意:我正在将代码从python 27转换为32

1 个答案:

答案 0 :(得分:5)

我必须这样做:

raise visa_exceptions.VisaIOError(status)