From charlesreid1

Revision as of 05:41, 30 May 2017 by Admin (talk | contribs)

Class exceptions:

Abstract datatype example - exceptions following specifications

The case of exception handling for an abstract data type like a stack is pretty clear-cut: there are two explicit error cases that it should handle, both when items are requested and the stack is empty. To raise these exceptions, we just do a variation of raise Exception("oops"):

class Empty(Exception):
    pass

Done. Now we can do raise Empty("oops, empty stack").

See StacksQueues/Python/ArrayStack#Class_implementation