473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about raise and exceptions.

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
Feb 28 '07 #1
7 1158
On Wed, 28 Feb 2007 13:48:54 -0500 (EST), "Steven W. Orr"
<st****@syslang.netwrote:
>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
Feb 28 '07 #2
Daniel Klein a écrit :
On Wed, 28 Feb 2007 13:48:54 -0500 (EST), "Steven W. Orr"
<st****@syslang.netwrote:

>>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 !-)

Feb 28 '07 #3
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"
=><st****@syslang.netwrote:
=>>
=>>
=>>>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
Feb 28 '07 #4
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"
=><st****@syslang.netwrote:
=>>
=>>
=>>>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
Feb 28 '07 #5
On Wed, 28 Feb 2007 22:03:13 +0100, Bruno Desthuilliers
<bd*****************@free.quelquepart.frwrote:
>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 !-)
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
Mar 1 '07 #6
On 1 mar, 04:46, Daniel Klein <danielklei...@gmail.comwrote:
On Wed, 28 Feb 2007 22:03:13 +0100, Bruno Desthuilliers

<bdesth.quelquech...@free.quelquepart.frwrote:
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 !-)

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.

>>t = 1, 2, 3
t
(1, 2, 3)
>>type(t)
<type 'tuple'>
>>t2 = 1,
t2
(1,)
>>def toto():
return "it's me"
....
>>toto
<function toto at 0xb7d18c34>
>>toto()
"it's me"
>>>


Mar 1 '07 #7
"br*****************@gmail.com" <br*****************@gmail.comwrites:
On 1 mar, 04:46, Daniel Klein <danielklei...@gmail.comwrote:
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.
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.

--
\ "My house is made out of balsa wood, so when I want to scare |
`\ the neighborhood kids I lift it over my head and tell them to |
_o__) get out of my yard or I'll throw it at them." -- Steven Wright |
Ben Finney

Mar 1 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Tony | last post by:
I am in the process of setting up a base page model for multiple reasons. One of the reasons is so that I can catch all exceptions when derived pages throw/raise them. I don't want to use the...
1
by: Terry Lee Tucker | last post by:
Hi, I have a simple question. I have been looking at the HTML docs for Postgres version: PostgreSQL 7.2.3-RH on i686-pc-linux-gnu, compiled by GCC 2.96. I need to know if it is necessary to...
21
by: Christophe | last post by:
Is there a good reason why when you try to take an element from an already exausted iterator, it throws StopIteration instead of some other exception ? I've lost quite some times already because I...
6
by: DarkBlue | last post by:
My application makes several connections to a remote database server via tcp/ip. Usually all is fine,but occasionally the server is down or the internet does not work and then there is the 30 sec...
5
by: Ed Jensen | last post by:
I'm really enjoying using the Python interactive interpreter to learn more about the language. It's fantastic you can get method help right in there as well. It saves a lot of time. With that...
2
by: crybaby | last post by:
Why you specify type and name of the exception in your custom exceptions, but not in built in exceptions except IOError: print "no file by the name ccy_rates*.txt" except MyError, e: print...
9
by: ssecorp | last post by:
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...
14
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.