- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to throw an exception into a continuation
Fri, 2012-02-17, 16:11
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...
Fri, 2012-02-17, 17:21
#2
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...
Fri, 2012-02-17, 17:41
#3
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...
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...