title: Quick failure when using for-comprehension with scala.util.Try
body: <p>I really like <code>scala.util.Try</code> in Scala 2.10, and how it works with for-comprehension makes handling multiple steps that could go wrong easily.</p>
  
  <p>For example, we could use the following code to make sure we only print out that two numbers if and only if everything is under control and we get value correctly.</p>
  
  <pre><code>def tryA: Try[Int] = {....}
  def tryB: Try[Int] = {....}
  
  for {
    a &lt;- tryA
    b &lt;- tryB
  } { 
    println (s"We got:${a+b}")
  }
  </code></pre>
  
  <p>But one of my concern is that this code is actually ignore any exceptions, which means it will looks like the following try-cactch block:</p>
  
  <pre><code>try {
    // .....
  } catch {
    case _: Exception =&gt; // Swallow any exception
  }
  </code></pre>
  
  <p>As far as I know, there is an argument that this kind of codes is a bad smell, because no one will notice there is an exception occurs. </p>
  
  <p>What I would like to achieve is that still using <code>for</code> to make sure the <code>println</code> only execute if everything is OK, but if there is any exception in any steps, it will blow up and throw out the exception directly.</p>
  
  <p>Currently here is how I do this, but it seems less elegant because it introduce a new <code>Try[Unit]</code> object, so I'm wondering how could I make this code better? </p>
  
  <p>For example, is it possible to get rid of the <code>result</code> variable and <code>result.get</code> statement, but still get exception being thrown?</p>
  
  <pre><code>def tryA: Try[Int] = {....}
  def tryB: Try[Int] = {....}
  
  val result = for {
    a &lt;- tryA
    b &lt;- tryB
  } yield { 
    println (s"We got:${a+b}")
  }
  result.get
  </code></pre>
  
  <h1>Update</h1>
  
  <p>To make thing more clear, it is the result from Scala REPL of the first code in this question.</p>
  
  <pre><code>scala&gt; def tryA: Try[Int] = Success(1)
  tryA: scala.util.Try[Int]
  
  scala&gt; def tryB: Try[Int] = Failure(new Exception("error"))
  tryB: scala.util.Try[Int]
  
  scala&gt; for {
       |   a &lt;- tryA
       |   b &lt;- tryB
       | } { 
       |   println (s"We got:${a+b}")
       | }
  
  scala&gt; 
  </code></pre>
  
  <p>We can see that nothing happens here, even <code>tryB</code> is a <code>Failure</code> with exception. What I would like to get is an exception being thrown, and without the introduce the new <code>Try[Unit]</code> object with <code>yield</code>, is this possible?</p>