title: Exception handling with scala futures
body: <p>I have a problem with Scala Futures. I have a function that takes two parameters: x: Future[Int] and y: Future[Int]. </p>
  
  <p>The function should return a future that completes with:</p>
  
  <ul>
  <li>value of x if x completes successfully,</li>
  <li>value of y if x fails and y completes successfully,</li>
  <li>the exception that failed y if x fails and y fails.</li>
  </ul>
  
  <p>I cannot seem to figure out how to do this.</p>
  
  <p>The code currently:</p>
  
  <pre><code>def myFunction(x: Future[Int], y: Future[Int]): Future[Int] = {
    x.onSuccess {
      case result =&gt; return Future(result)
    }
    x.onFailure {
      case e =&gt; 
        y.onSuccess {
          case res =&gt; return Future(res)
        }
        y.onFailure {
          case f =&gt; throw f
        }
    }
  }
  </code></pre>