Correct Method of Exception handling

1 view
Skip to first unread message

Jay Herrmann

unread,
Sep 23, 2010, 5:52:42 PM9/23/10
to pysage
Hi John,

Do you have any thoughts on best practices for exception handling in
the Actors??

I was wondering if the Actors have an exception handler method.

Like:
class Player(Actor):
subscriptions = ['BombMessage']
def handle_BombMessage(self, msg):
print 'I took %s damage from the bomb' %
msg.get_property('damage')
def Exception(self,exception)
handled = true

Or maybe in the MainGroup sometime similar. So the if a child group
process dies because of an exception the ChidlGroup or MainGroup
could restart it.

I know my coding is not perfect and could have hidden exceptions. And
since I am running this on an embedded system. It would nice if the
app could restart its child processes.


Jay.

John Yang

unread,
Sep 24, 2010, 12:47:18 AM9/24/10
to pys...@googlegroups.com
Jay:

If you expect an exception to be raised in your message handlers. You
need to handle that with a "try/except" yourself, like this:

class Play(Actor):


subscriptions = ['BombMessage']
def handle_BombMessage(self, msg):

try:
# do something with the bomb
pass
exception Exception, e:
print "oops"

If you want to catch when a child group had failed, on your main
group's "tick", you can catch for a "GroupFailed" exception. Then you
can do the appropriate removing of the group and re-enabling it.

so in your main group, something like:

while True:
try:
mgr.tick()
except GroupFailed, e:
# remove your group and re-enable it
pass

There is a unit test that tests for this exception to be raised in
here (line 132):

http://code.google.com/p/pysage/source/browse/trunk/tests/test_groups_process.py#132

Does that help?

John

> --
> You received this message because you are subscribed to the Google Groups "pysage" group.
> To post to this group, send email to pys...@googlegroups.com.
> To unsubscribe from this group, send email to pysage+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pysage?hl=en.
>
>

Jay Herrmann

unread,
Sep 24, 2010, 8:57:13 PM9/24/10
to pys...@googlegroups.com
Yes that helps.  I did not know if the I had to put the try except in all my methods in the Actor class.  And how to catch the exceptions in the MainGroup.  So if a ClientGroup dies then i do the following????

try
    mgr.tick()
except GroupFailed e:
    mgr.remove_process_group(name)  # Do we have the group name
    mgr.add_process_group(name,Class)

John Yang

unread,
Sep 27, 2010, 12:54:20 AM9/27/10
to pys...@googlegroups.com
On Fri, Sep 24, 2010 at 7:57 PM, Jay Herrmann <jay.he...@gmail.com> wrote:
> Yes that helps.  I did not know if the I had to put the try except in all my
> methods in the Actor class.

Jay:

handlers are dispatched via "handle_message". If you want to
implement a "catch all", you could do something like this:

class Play(Actor):
   subscriptions = ['BombMessage']

   def handle_message(self, msg):
try:
Actor.handle_message(self, msg)
except Exception, e:
print e

> And how to catch the exceptions in the
> MainGroup.  So if a ClientGroup dies then i do the following????
>
> try
>     mgr.tick()
> except GroupFailed e:
>     mgr.remove_process_group(name)  # Do we have the group name
>     mgr.add_process_group(name,Class)

Please update to pysage 1.5.5-1.

I added the ability to inspect the group's name on exception. See

http://code.google.com/p/pysage/source/browse/trunk/tests/test_groups_process.py#140

Let me know if that helps.

Jay Herrmann

unread,
Sep 27, 2010, 1:51:20 PM9/27/10
to pys...@googlegroups.com
Hi John,

Yes, this is the info I wondering about.


I added the ability to inspect the group's name on exception.  See

http://code.google.com/p/pysage/source/browse/trunk/tests/test_groups_process.py#140

Thanks for the update of the e to include the Group name.  I think I can provide a solid application to my embedded.  All made possible be pysage and your hard work.  

Thank again,
Jay 
Reply all
Reply to author
Forward
0 new messages