【问题标题】:I get "TypeError: exceptions must derive from BaseException" even though I did define it我得到“TypeError:异常必须从 BaseException 派生”,即使我确实定义了它
【发布时间】:2013-07-23 19:29:32
【问题描述】:

根据 python 文档,异常是从 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)

但我得到(引用 sn-p):

   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

【问题讨论】:

    标签: python exception


    【解决方案1】:

    我必须这样做:

    raise visa_exceptions.VisaIOError(status)
    

    【讨论】:

    • 是的。你试图提出一个元组:(visa_exceptions.VisaIOError, status)。这就是TypeError 出现的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2021-08-02
    相关资源
    最近更新 更多