473,770 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_stat e:
raise TransitionError , self.curr_state , newstate, \
"Going to error state %d from state %d" % (self.curr_stat e, 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_stat e, 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 1171
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_stat e, newstate)
^
SyntaxError: invalid syntax
The arguments for TransitionError must be a tuple, eg:

msg = "Going to error state %d from state %d" % (self.curr_stat e,
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_stat e, 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_stat e,
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****@sysla ng.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_stat e, 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_stat e,
=>newstate)
=>raise TransitionError (self, curr_state, newstate, msg)
=>
=>Where did you see a tuple here ? You're code is *calling*
=>TransitionErr or, 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****@sysla ng.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_stat e, 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_stat e,
=>newstate)
=>raise TransitionError (self, curr_state, newstate, msg)
fix:

should be:
raise TransitionError (self.curr_stat e, newstate, msg)
or
raise TransitionError , (self.curr_stat e, 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_stat e, newstate, msg)
vs
raise TransitionError , (self.curr_stat e, newstate, msg)

HTH
Feb 28 '07 #5
On Wed, 28 Feb 2007 22:03:13 +0100, Bruno Desthuilliers
<bd************ *****@free.quel quepart.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_stat e,
newstate)
raise TransitionError (self, curr_state, newstate, msg)

Where did you see a tuple here ? You're code is *calling*
TransitionErro r, 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.quelque ch...@free.quel quepart.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_stat e,
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.com writes:
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
4228
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
1428
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 standard OnError virtual method to handle this requirement because the form does not complete its rendering process when this occurs. I really need the form to display properly on the screen. I have managed to catch nearly all exceptions by...
1
2848
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 RETURN NULL from a trigger function after raising an exception with RAISE EXCEPTION. We are doing many different data validations in a "BEFORE UPDATE OR INSERT" trigger and we have been raising an exception with an error string such that the...
21
1412
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 was using a lot of iterators and I forgot that that specific function parameter was one. Exemple : >>> def f(i): .... print list(i) .... print list(i)
6
1678
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 to several minutes timeout wait for the tcp to give up. Is there anything I can do without using threads,sockets,twisted etc. to have following : pseudocode :
5
1294
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 in mind, is there an easy way in the interactive interpreter to determine which exceptions a method might raise? For example, it would be handy if there was something I could do in the interactive interpreter to make it tell me what exceptions...
2
1399
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 e.msg
9
5170
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 __init__(self, *items): self.stack = list(items) def push(self, item): self.stack.append(item)
14
4358
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 ignored when called. I can see the attribute using dir(self.__class__) on an instance, but when called, python enters __getattr__. If I correct the bug, the attribute calls work as expected and do not call __getattr__. I can't seem to make a simple...
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.