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

How to throw an exception into a continuation

3 replies
daniel.kroeni
Joined: 2008-09-30,
User offline. Last seen 1 year 46 weeks ago.

Hi all

I try to implement proper exception handling for scala-async [1] (C#
async in scala).
Does anybody know a clean solution for the ugly part below (also here
[2])?

def await[A](block: => Future[A]): A@suspendable = {
val ec = localContext get()
val ex = new AtomicReference[Throwable] // holds the exception if
any
val a = shift[A,Unit,Unit] { cont: (A => Unit) =>
block onComplete {
case Right(r) => ec.execute(cont(r))
case Left(e) =>
ex.set(e) // store exception
ec.execute(cont(null.asInstanceOf[A])) //TODO solve
appropriately
}
}
// rethrow exception if any
if(ex.get() != null) throw ex.get()
a
}

Any comments are welcome.

Thanks Daniel

[1] https://github.com/danielkroeni/scala-async/
[2] https://github.com/danielkroeni/scala-async/blob/master/src/main/scala/a...

Tiark Rompf
Joined: 2009-02-18,
User offline. Last seen 42 years 45 weeks ago.
Re: How to throw an exception into a continuation

def await[A](block: => Future[A]): A@suspendable = {
val ec = localContext get()
val a = shift { cont: (Either[Throwable,A] => Unit) =>
block onComplete { a => ec.execute(cont(a)) }
}
a match {
case Right(r) => r
case Left(e) => throw e
}
}

On Feb 17, 2012, at 4:11 PM, Daniel wrote:

> Hi all
>
> I try to implement proper exception handling for scala-async [1] (C#
> async in scala).
> Does anybody know a clean solution for the ugly part below (also here
> [2])?
>
> def await[A](block: => Future[A]): A@suspendable = {
> val ec = localContext get()
> val ex = new AtomicReference[Throwable] // holds the exception if
> any
> val a = shift[A,Unit,Unit] { cont: (A => Unit) =>
> block onComplete {
> case Right(r) => ec.execute(cont(r))
> case Left(e) =>
> ex.set(e) // store exception
> ec.execute(cont(null.asInstanceOf[A])) //TODO solve
> appropriately
> }
> }
> // rethrow exception if any
> if(ex.get() != null) throw ex.get()
> a
> }
>
> Any comments are welcome.
>
> Thanks Daniel
>
> [1] https://github.com/danielkroeni/scala-async/
> [2] https://github.com/danielkroeni/scala-async/blob/master/src/main/scala/a...

daniel.kroeni
Joined: 2008-09-30,
User offline. Last seen 1 year 46 weeks ago.
Re: How to throw an exception into a continuation

This second I run out of the shower to try exactly this. Many thanks!

On Feb 17, 5:06 pm, Tiark Rompf wrote:
> def await[A](block: => Future[A]): A@suspendable = {
>    val ec = localContext get()
>    val a = shift { cont: (Either[Throwable,A] => Unit) =>
>      block onComplete { a => ec.execute(cont(a)) }
>    }
>    a match {
>       case Right(r) => r
>       case Left(e) => throw e
>    }
>  }
>
> On Feb 17, 2012, at 4:11 PM, Daniel wrote:
>
>
>
>
>
>
>
> > Hi all
>
> > I try to implement proper exception handling for scala-async [1] (C#
> > async in scala).
> > Does anybody know a clean solution for the ugly part below (also here
> > [2])?
>
> > def await[A](block: => Future[A]): A@suspendable = {
> >    val ec = localContext get()
> >    val ex = new AtomicReference[Throwable] // holds the exception if
> > any
> >    val a = shift[A,Unit,Unit] { cont: (A => Unit) =>
> >      block onComplete {
> >        case Right(r) => ec.execute(cont(r))
> >        case Left(e) =>
> >          ex.set(e) // store exception
> >          ec.execute(cont(null.asInstanceOf[A])) //TODO solve
> > appropriately
> >      }
> >    }
> >    // rethrow exception if any
> >    if(ex.get() != null) throw ex.get()
> >    a
> >  }
>
> > Any comments are welcome.
>
> > Thanks Daniel
>
> > [1]https://github.com/danielkroeni/scala-async/
> > [2]https://github.com/danielkroeni/scala-async/blob/master/src/main/scal...

daniel.kroeni
Joined: 2008-09-30,
User offline. Last seen 1 year 46 weeks ago.
Re: How to throw an exception into a continuation

Tiark
Many thanks again. I updated the code [1]. Now it looks quite nice.

[1] https://github.com/danielkroeni/scala-async/commit/134d7d859b2f24f3204c2...

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