Question about raise and exceptions.

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Feb 28 16:48:35 EST 2007


"Steven W. Orr" <steveo at syslang.net> writes:

> In my class I have

I assume you mean "in my module", since you're showing us several
classes.

>      class TransitionError(Error):
> [...]
>          def __init__(self, previous, next, message):

Here you define the arguments that must be passed when creating a
TransitionError instance.

> Also in my class I check to see if a transition is legal or not:

I assume this code is in a different class elsewhere.

>              if newstate == self.error_state:
>                  raise TransitionError, self.curr_state, newstate, \
> "Going to error state %d from state %d" % (self.curr_state, newstate)

Here you specify the *class* TransitionError, instead of creating a
new *instance* of TransitionError with the required arguments. This
should be, as you specified in the definition of the TransitionError
class:

        raise TransitionError(self.curr_state, newstate,
                "Badness: %d from %d" % (self.curr_state, newstate)
                )

The syntax of the 'raise' statement requires one, two, or three
arguments, all of which instruct 'raise' what to do, and none of which
refer to arguments given to the exception instance.

    <URL:http://docs.python.org/ref/raise.html#raise>

The above statement will supply one argument, an instance to raise as
the exception. The other forms of the 'raise' statement are all about
what raise should do, so your example above, even if it were not
incorrect syntax, is logically incorrect.

-- 
 \       "I believe in making the world safe for our children, but not |
  `\    our children's children, because I don't think children should |
_o__)                                  be having sex."  -- Jack Handey |
Ben Finney




More information about the Python-list mailing list