title: Most idiomatic way to write try/catch/finally in Scala?
body: <p>What is the best way to write the following in Scala? It doesn't look quite right to me - first the forward declaration of the 2 vals, then the long <code>PrintWriter</code> creation line, then the<code>finally</code>block. The only thing that's idiomatic, is the <code>catch</code> block...</p>
  
  <pre><code>val outputStream = Try(fs.create(tmpFile))
  val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream.get)))
  
  if (outputStream.isFailure) {
    logger.error(s"Couldn't open file: $tmpFile")
  }
  
  try {
  
    features.foreach {
      case (sectionName, modelRDD) =&gt;
        writer.append("{" + sectionName + ", " + modelRDD.getNumPartitions + "}")
    }
  
  } catch {
    case e: Exception =&gt;
      logger.error(s"Got exception", e)
      throw e
  
  } finally {
    outputStream.get.close()
    writer.close()
  }
  </code></pre>