This page is no longer maintained — Please continue to the home page at www.scala-lang.org

actor exception handling

4 replies
linxbetter
Joined: 2009-03-28,
User offline. Last seen 3 years 31 weeks ago.
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String =>   val res = doSomethingWithJson(json)   reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great.  The problem comes when doSomethingWithJson might throw an exception.  In particular I have a unit test where I'd like to be able to do the following:
def test = {  { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten.  Furthermore my test freezes so it appears the actor is not even active anymore.  I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: actor exception handling
You should either

 1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)

 2) Create a supervisor actor and link it to "a".  The supervisor actors should catch the Exit message and handle it appropriately.

In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.

- Josh

On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String =>   val res = doSomethingWithJson(json)   reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great.  The problem comes when doSomethingWithJson might throw an exception.  In particular I have a unit test where I'd like to be able to do the following:
def test = {  { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten.  Furthermore my test freezes so it appears the actor is not even active anymore.  I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln

linxbetter
Joined: 2009-03-28,
User offline. Last seen 3 years 31 weeks ago.
Re: actor exception handling
Josh thanks for the info.
Really I'm only interested in !? for my tests.  I can't think of a reason I wouldn't be calling ! all the time in production.  Can you show me how to do #2?
Thanks,Lincoln

On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
You should either

 1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)

 2) Create a supervisor actor and link it to "a".  The supervisor actors should catch the Exit message and handle it appropriately.

In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.

- Josh

On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String =>   val res = doSomethingWithJson(json)   reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great.  The problem comes when doSomethingWithJson might throw an exception.  In particular I have a unit test where I'd like to be able to do the following:
def test = {  { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten.  Furthermore my test freezes so it appears the actor is not even active anymore.  I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln


linxbetter
Joined: 2009-03-28,
User offline. Last seen 3 years 31 weeks ago.
Re: actor exception handling
Also, will an uncaught exception cause the actor to stop accepting messages?

On Sun, Oct 4, 2009 at 8:15 AM, Lincoln <linxbetter@gmail.com> wrote:
Josh thanks for the info.
Really I'm only interested in !? for my tests.  I can't think of a reason I wouldn't be calling ! all the time in production.  Can you show me how to do #2?
Thanks,Lincoln

On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
You should either

 1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)

 2) Create a supervisor actor and link it to "a".  The supervisor actors should catch the Exit message and handle it appropriately.

In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.

- Josh

On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String =>   val res = doSomethingWithJson(json)   reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great.  The problem comes when doSomethingWithJson might throw an exception.  In particular I have a unit test where I'd like to be able to do the following:
def test = {  { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten.  Furthermore my test freezes so it appears the actor is not even active anymore.  I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln



linxbetter
Joined: 2009-03-28,
User offline. Last seen 3 years 31 weeks ago.
Re: actor exception handling
I tested and it seems like the answer is no which is good.  I've decided to wrap my actor bodies in try/catch and return an Either.  In my tests I call them using !? and otherwise I call them using !.
Is there any way within the body of an actor to know how it was called?  I ask because my actors also pass messages to other actors and ideally, if I'm calling from a test I would like everything to be synchronous so that I can test results reliably.
Thanks,Lincoln

On Sun, Oct 4, 2009 at 8:16 AM, Lincoln <linxbetter@gmail.com> wrote:
Also, will an uncaught exception cause the actor to stop accepting messages?

On Sun, Oct 4, 2009 at 8:15 AM, Lincoln <linxbetter@gmail.com> wrote:
Josh thanks for the info.
Really I'm only interested in !? for my tests.  I can't think of a reason I wouldn't be calling ! all the time in production.  Can you show me how to do #2?
Thanks,Lincoln

On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
You should either

 1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)

 2) Create a supervisor actor and link it to "a".  The supervisor actors should catch the Exit message and handle it appropriately.

In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.

- Josh

On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String =>   val res = doSomethingWithJson(json)   reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great.  The problem comes when doSomethingWithJson might throw an exception.  In particular I have a unit test where I'd like to be able to do the following:
def test = {  { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten.  Furthermore my test freezes so it appears the actor is not even active anymore.  I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln




Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland