Correct use of try,except and raise?

ssecorp circularfunc at gmail.com
Sat Jul 12 19:31:55 EDT 2008


Is this correct use of exceptions? to raise an indexerror and add my
own string insetad of just letting it raise a IndexError by itself and
"blaming" it on list.pop?

class Stack(object):
    def __init__(self, *items):
        self.stack = list(items)

    def push(self, item):
        self.stack.append(item)

    def pop(self):
        try:
            return self.stack.pop()
        except:
            raise IndexError, "pop from empty stack"

class Queue(object):
    def __init__(self, *items):
        self.queue = list(items)

    def append(self, item):
        self.queue.append(item)

    def pop(self):
        try:
            return self.queue.pop(0)
        except:
            raise IndexError, "pop from empty queue"



More information about the Python-list mailing list