Python 创建合作的例外情况

Python 创建合作的例外情况,python,exception,custom-exceptions,Python,Exception,Custom Exceptions,Python状态: 程序可以通过创建新异常来命名自己的异常 类(有关Python类的更多信息,请参见类)。例外情况应 通常直接或从Exception类派生 间接地 创建可能引发多个不同错误的模块时 通常的做法是为定义的异常创建基类 该模块以及用于创建特定异常类的子类 对于不同的错误条件 发件人: 每个级别都会去掉它需要的关键字参数,以便 可以将最终的空dict发送到不需要任何参数的方法 all(例如,object.init需要零个参数) 假设我有以下StudentValueError和Miss

Python状态:

程序可以通过创建新异常来命名自己的异常 类(有关Python类的更多信息,请参见类)。例外情况应 通常直接或从Exception类派生 间接地

创建可能引发多个不同错误的模块时 通常的做法是为定义的异常创建基类 该模块以及用于创建特定异常类的子类 对于不同的错误条件

发件人:

每个级别都会去掉它需要的关键字参数,以便 可以将最终的空dict发送到不需要任何参数的方法 all(例如,object.init需要零个参数)

假设我有以下
StudentValueError
MissingStudentValue
异常

class StudentValueError(Exception):
    """Base class exceptions for Student Values"""
    def __init__(self, message, **kwargs):
        super().__init__(**kwargs)
        self.message = message # You must provide at least an error message.


class MissingStudentValue(StudentValueError):
    def __init__(self, expression, message, **kwargs):
        super().__init__(message, **kwargs)
        self.expression = expression

    def __str__(self):
        return "Message: {0} Parameters: {1}".format(self.message, self.expression)
我想创建一些合作的例外情况。我有两个问题:

  • 在这种情况下,
    异常
    类构造函数需要零个参数(空的
    dict
    ),对吗
  • 我的例子是否违反了LSP

  • 提供的接受答案继承自
    ValueError
    异常
    不接受关键字参数,它只通过
    *args
    接受可变数量的位置参数,因此您需要将
    **kwargs
    更改为
    *args
    。另外,我建议将
    消息
    表达式
    *args
    一起传递给
    super()
    调用。毕竟,这个例子可能并不违反LSP:

    class StudentValueError(Exception):
        """Base class exceptions for Student Values"""
        def __init__(self, message='', *args):
            super().__init__(message, *args)
            self.message = message 
    
    
    class MissingStudentValue(StudentValueError):
        def __init__(self, message='', expression='', *args):
            super().__init__(message, expression, *args)
            self.expression = expression
    
        def __str__(self):
            return "Message: {0} Parameters: {1}".format(self.message, self.expression)
    
    
    e = Exception('message', 'expression', 'yet_another_argument')
    print(e)
    e = StudentValueError('message', 'expression', 'yet_another_argument')
    print(e)
    e = MissingStudentValue('message', 'expression', 'yet_another_argument')
    print(e)
    e = MissingStudentValue()
    print(e)
    

    异常
    不接受关键字参数,它通过
    *args
    只接受可变数量的位置参数,因此您需要将
    **kwargs
    更改为
    *args
    。另外,我建议将
    消息
    表达式
    *args
    一起传递给
    super()
    调用。毕竟,这个例子可能并不违反LSP:

    class StudentValueError(Exception):
        """Base class exceptions for Student Values"""
        def __init__(self, message='', *args):
            super().__init__(message, *args)
            self.message = message 
    
    
    class MissingStudentValue(StudentValueError):
        def __init__(self, message='', expression='', *args):
            super().__init__(message, expression, *args)
            self.expression = expression
    
        def __str__(self):
            return "Message: {0} Parameters: {1}".format(self.message, self.expression)
    
    
    e = Exception('message', 'expression', 'yet_another_argument')
    print(e)
    e = StudentValueError('message', 'expression', 'yet_another_argument')
    print(e)
    e = MissingStudentValue('message', 'expression', 'yet_another_argument')
    print(e)
    e = MissingStudentValue()
    print(e)
    

    谢谢你的回答。它是否在文档中的任何地方说明了
    Exception
    采用
    *args
    ?因为连链接的答案都使用
    **kwargs
    。我问题中的例子是否违反LSP?不幸的是,我在文档中找不到关于它的信息。我在运行时对它进行了测试:尝试传递一个关键字参数,如
    e=MissingStudentValue('message','expression',something='something')
    会导致一个错误:
    TypeError:Exception在调用
    super()
    时不接受关键字参数,但我的示例是否违反了LSP?是的,我意识到我的回答也不正确。需要将默认值传递给所有参数,因为基类
    Exception
    可以在没有任何参数的情况下实例化,例如
    e=Exception()
    。我更新了我的答案,现在自定义异常也可以在不传递任何参数的情况下进行初始化(参见上一个示例)。因此,您的代码违反了,因为
    e=Exception()
    可以完成,但
    e=MissingStudentValue()无法完成,它将引发
    TypeError:\uuu init\uuu()缺少2个必需的位置参数:“message”和“expression”
    谢谢您的回答。它是否在文档中的任何地方说明了
    Exception
    采用
    *args
    ?因为连链接的答案都使用
    **kwargs
    。我问题中的例子是否违反LSP?不幸的是,我在文档中找不到关于它的信息。我在运行时对它进行了测试:尝试传递一个关键字参数,如
    e=MissingStudentValue('message','expression',something='something')
    会导致一个错误:
    TypeError:Exception在调用
    super()
    时不接受关键字参数,但我的示例是否违反了LSP?是的,我意识到我的回答也不正确。需要将默认值传递给所有参数,因为基类
    Exception
    可以在没有任何参数的情况下实例化,例如
    e=Exception()
    。我更新了我的答案,现在自定义异常也可以在不传递任何参数的情况下进行初始化(参见上一个示例)。因此,您的代码违反了,因为
    e=Exception()
    可以完成,但
    e=MissingStudentValue()无法完成,它将引发
    TypeError:\uuu init\uuu()缺少2个必需的位置参数:“消息”和“表达式”