title: How do I get hold of exceptions thrown in a Scala Future?
body: <p>I've been working up my answer to <a href="https://stackoverflow.com/q/6227759/97777">Is there a standard Scala function for running a block with a timeout?</a>, and have run into a problem if an exception is thrown in a Future.</p>
  
  <pre><code>  def runWithTimeout[T](timeoutMs: Long)(f: =&gt; T) : Option[T] = {
      awaitAll(timeoutMs, future(f)).head.asInstanceOf[Option[T]]
    }
  </code></pre>
  
  <p>So that</p>
  
  <pre><code>runWithTimeout(50) { "result" } should equal (Some("result"))
  runWithTimeout(50) { Thread.sleep(100); "result" } should equal (None)
  </code></pre>
  
  <p>But if I throw an exception in my block it doesn't leak, but is swallowed - so that the following fails with "..no exception was thrown"</p>
  
  <pre><code>intercept[Exception] {
      runWithTimeout(50) { throw new Exception("deliberate") }
  }.getMessage should equal("deliberate")
  </code></pre>
  
  <p>Syserr has a stack trace with the message </p>
  
  <pre><code>&lt;function0&gt;: caught java.lang.Exception: deliberate
  </code></pre>
  
  <p>but I can't find where in the Scala runtime that is printed.</p>
  
  <p>Apart from wrapping f in another block which catches exceptions and propagates them if thrown, is there any way to persuade awaitAll and/or Future to throw?</p>