Create a base class for exceptions defined by a module : Exception Class « Exception « Python






Create a base class for exceptions defined by a module

# create specific exception classes for different error conditions:

class Error(Exception):
    """Base class for exceptions in this module."""
    pass

class InputError(Error):
    """Exception raised for errors in the input.

    Attributes:
        expression -- input expression in which the error occurred
        message -- explanation of the error
    """

    def __init__(self, expression, message):
        self.expression = expression
        self.message = message

class TransitionError(Error):
    """Raised when an operation attempts a state transition that's not
    allowed.

    Attributes:
        previous -- state at beginning of transition
        next -- attempted new state
        message -- explanation of why the specific transition is not allowed
    """

    def __init__(self, previous, next, message):
        self.previous = previous
        self.next = next
        self.message = message


           
       








Related examples in the same category

1.A programmer-defined exception class.A programmer-defined exception class.
2.User-defined Exceptions: Name their own exceptions by creating a new exception classUser-defined Exceptions: Name their own exceptions by creating a new exception class