28

What is the analog for .Net InvalidOperationException in Python?

Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90

4 Answers4

22

There's no direct equivalent. Usually ValueError or TypeError suffices, perhaps a RuntimeError or NotImplementedError if neither of those fit well.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
10

I'd probably go between one of two options:

  1. A custom exception, best defined as follows:

    class InvalidOperationException(Exception): pass

  2. Just using Exception

I don't believe there's a direct analogue; Python seems to have a very flat exception hierarchy.

Chris R
  • 17,546
  • 23
  • 105
  • 172
4

I'll partially agree with Chris R -- define your own:

     class InvalidOperationException(Exception): pass

You get much benefit from defining your own exceptions this way, including building a hierarchy to fit your needs:

     class MyExceptionBase(Exception): pass
     class MyExceptionType1(MyExceptionBase): pass
     class MyExceptionType2(MyExceptionBase): pass
     # ...
     try:
        # something
     except MyExceptionBase, exObj:
        # handle several types of MyExceptionBase here...

I don't agree with throwing a naked "Exception", though.

Kevin Little
  • 12,436
  • 5
  • 39
  • 47
0

In my opinion, you should do

return NotImplemented

instead of defining your own exception. See the example below for a case where this would be preferable:

class InvalidOperationException(Exception):
    pass


class Vector2D(list):
    def __init__(self, a, b):
        super().__init__([a, b])

    def __add__(self, other):
        return Vector2D(self[0] + other[0],
                        self[1] + other[1])

    def __mul__(self, other):
        if hasattr(other, '__len__') and len(other) == 2:
            return self[0]*other[0] + self[1]*other[1]
        return Vector2D(self[0]*other, self[1]*other)

    def __rmul__(self, other):
        if hasattr(other, '__len__') and len(other) == 2:
            return Vector2D(other[0]*self, other[1]*self)
        return Vector2D(other*self[0], other*self[1])


class Matrix2D(list):
    def __init__(self, v1, v2):
        super().__init__([Vector2D(v1[0], v1[1]),
                          Vector2D(v2[0], v2[1])])

    def __add__(self, other):
        return Matrix2D(self[0] + other[0],
                        self[1] + other[1])

    def __mul__(self, other):
        # Change to:
        # return InvalidOperationException
        # or:
        # raise InvalidOperationException
        return NotImplemented


if __name__ == '__main__':

    m = Matrix2D((1, -1),
                 (0,  2))

    v = Vector2D(1, 2)

    assert v*v == 5  # [1 2] [1] = 5
    #                        [2]

    assert v*m == Vector2D(1, 3)  # [1 2] [1 -1] = [1 3]
    #                                     [0  2]

    try:
        result = m*m
        print('Operation should have raised a TypeError exception, '
              'but returned %s as a value instead' % str(result))
    except TypeError:
        pass

    assert m*v == Vector2D(-1, 4)  # [1 -1] [1] = [-1]
    #                                [0  2] [2]   [ 4]

It should run without any error. When doing m*v, it tries to call __mul__ from Matrix2D, but fails. If, and only if, it returns NotImplemented, then it tries to call __rmul__ from the object on the right side.

Wood
  • 271
  • 1
  • 8