Question about raise and exceptions.

S

Steven W. Orr

In my class I have

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

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

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

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

When I run it I get this:

884 > ./t_fsm.py
Traceback (most recent call last):
File "./t_fsm.py", line 3, in ?
from fsm import *
File "/home/boston/VIASAT/sorr/py/fsm/fsm.py", line 76
raise TransitionError, self.curr_state, newstate, "Going to error
state %d from state %d" % (self.curr_state, newstate)
^
SyntaxError: invalid syntax

(The carat is really under the comma before "Going.)

I hate to bother people with syntax problems, but I have no idea what to
do. Sorry.

TIA :-(

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
D

Daniel Klein

When I run it I get this:

884 > ./t_fsm.py
Traceback (most recent call last):
File "./t_fsm.py", line 3, in ?
from fsm import *
File "/home/boston/VIASAT/sorr/py/fsm/fsm.py", line 76
raise TransitionError, self.curr_state, newstate, "Going to error
state %d from state %d" % (self.curr_state, newstate)
^
SyntaxError: invalid syntax

The arguments for TransitionError must be a tuple, eg:

msg = "Going to error state %d from state %d" % (self.curr_state,
newstate)
raise TransitionError(self, curr_state, newstate, msg)

Dan
 
B

Bruno Desthuilliers

Daniel Klein a écrit :
The arguments for TransitionError must be a tuple,
Err...

eg:

msg = "Going to error state %d from state %d" % (self.curr_state,
newstate)
raise TransitionError(self, curr_state, newstate, msg)

Where did you see a tuple here ? You're code is *calling*
TransitionError, passing it the needed arguments.

Note that it's the correct syntax - but not the correct explanation !-)
 
S

Steven W. Orr

On Wednesday, Feb 28th 2007 at 22:03 +0100, quoth Bruno Desthuilliers:

=>Daniel Klein a ?crit :
=>> On Wed, 28 Feb 2007 13:48:54 -0500 (EST), "Steven W. Orr"
=>>
=>>
=>>>When I run it I get this:
=>>>
=>>>884 > ./t_fsm.py
=>>>Traceback (most recent call last):
=>>> File "./t_fsm.py", line 3, in ?
=>>> from fsm import *
=>>> File "/home/boston/VIASAT/sorr/py/fsm/fsm.py", line 76
=>>> raise TransitionError, self.curr_state, newstate, "Going to error
=>>>state %d from state %d" % (self.curr_state, newstate)
=>>> ^
=>>>SyntaxError: invalid syntax
=>>
=>>
=>> The arguments for TransitionError must be a tuple,
=>
=>Err...
=>
=>> eg:
=>>
=>> msg = "Going to error state %d from state %d" % (self.curr_state,
=>> newstate)
=>> raise TransitionError(self, curr_state, newstate, msg)
=>
=>Where did you see a tuple here ? You're code is *calling*
=>TransitionError, passing it the needed arguments.
=>
=>Note that it's the correct syntax - but not the correct explanation !-)

Ok. Now I'm potentially confused:
1. TransitionError takes 4 args
2. raise takes a tuple with four elements after the exception argument as
its 2nd arg

I take it you mean #2?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
B

Bruno Desthuilliers

Steven W. Orr a écrit :
On Wednesday, Feb 28th 2007 at 22:03 +0100, quoth Bruno Desthuilliers:

=>Daniel Klein a ?crit :
=>> On Wed, 28 Feb 2007 13:48:54 -0500 (EST), "Steven W. Orr"
=>>
=>>
=>>>When I run it I get this:
=>>>
=>>>884 > ./t_fsm.py
=>>>Traceback (most recent call last):
=>>> File "./t_fsm.py", line 3, in ?
=>>> from fsm import *
=>>> File "/home/boston/VIASAT/sorr/py/fsm/fsm.py", line 76
=>>> raise TransitionError, self.curr_state, newstate, "Going to error
=>>>state %d from state %d" % (self.curr_state, newstate)
=>>> ^
=>>>SyntaxError: invalid syntax
=>>
=>>
=>> The arguments for TransitionError must be a tuple,
=>
=>Err...
=>
=>> eg:
=>>
=>> msg = "Going to error state %d from state %d" % (self.curr_state,
=>> newstate)
=>> raise TransitionError(self, curr_state, newstate, msg)

fix:

should be:
raise TransitionError(self.curr_state, newstate, msg)
or
raise TransitionError, (self.curr_state, newstate, msg)

(snip)
Ok. Now I'm potentially confused:
1. TransitionError takes 4 args

Almost (cf fix above).
2. raise takes a tuple with four elements after the exception argument as
its 2nd arg
No.

I take it you mean #2?
No.

the raise statement syntax is:
"raise" [typeOrInstance ["," value ["," traceback]]]

Most exceptions only take a single value (the error message), and it's
very uncommon (at least in application code) to substitute another
traceback.

Now back to the "value" part. If your exception type expects more than
one arg, it's legal to use a tuple for the value(s). It will then be
passed to the exception type (using *args expansion). It's legal, but it
doesn't make the code much more readable. It's IMHO better to
explicitely instanciate the exception with the needed params. Which of
these two lines do you find the most readable ?

raise TransitionError(self.curr_state, newstate, msg)
vs
raise TransitionError, (self.curr_state, newstate, msg)

HTH
 
D

Daniel Klein

Daniel Klein a écrit :

Where did you see a tuple here ? You're code is *calling*
TransitionError, passing it the needed arguments.

Note that it's the correct syntax - but not the correct explanation !-)

My bad :-(

Thanks for setting me straight. I had (wrongly) thought that the stuff
inside of () was a tuple.

To the OP: Please accept my apology for providing incorrect
information.

Dan
 
B

bruno.desthuilliers

My bad :-(

Thanks for setting me straight. I had (wrongly) thought that the stuff
inside of () was a tuple.

For the record, it's the ',' that makes the tuple. The () are (in
this context) the call operator.

 
B

Ben Finney

For the record, it's the ',' that makes the tuple. The () are (in
this context) the call operator.

For the further record, in the context of a function call, the ','
isn't making a tuple. It's separating the arguments to the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top