title: Scala most elegant way to handle option and throw exception from Scala Map
body: <p>I have a map and want to:</p>
  
  <ul>
  <li>retrieve value without handling Option  </li>
  <li>log a message when there is no such the key.</li>
  <li>nice to return a default value ( in addition to log a message) when the key is not present. This is optional because when the code fails here, it should not continue further.</li>
  </ul>
  
  <p>I have several ways of doing it</p>
  
  <pre><code>val map: Map[String, Int] // defined as such for simplification
  
  // 1 short but confusing with error handling
  def getValue(key: String): Int = {
      map.getOrElse(key, scala.sys.error(s"No key '$key' found"))
  }
  
  // in case you don't know scala.sys.error 
  package object sys {
   def error(message: String): Nothing = throw new RuntimeException(message)
  }
  </code></pre>
  
  <hr>
  
  <pre><code>// 2 verbose
  def getValue(key: String): Int = {
      try {
        map(key)
      } catch {
        case _: Throwable =&gt; scala.sys.error(s"No key '$key' found")
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>// 3 Try and pattern matching
  import scala.util.{Failure, Success, Try}
  
  def getValue(key: String): Int = {
      Try(map(key)) match{
        case Success(value) =&gt; value
        case Failure(ex) =&gt; sys.error(s"No key '$key' found ${ex.getMessage}")
      }
  }
  </code></pre>
  
  <p><strong>Others solutions are welcomed.</strong>
  I'm looking for conciseness without being cryptic. Using standard scala ( no need for Scalaz, Shapeless ... )</p>
  
  <p>Inspirations :</p>
  
  <ul>
  <li><a href="https://alvinalexander.com/scala/how-to-access-map-values-getorelse-scala-cookbook" rel="nofollow noreferrer">https://alvinalexander.com/scala/how-to-access-map-values-getorelse-scala-cookbook</a></li>
  <li><a href="http://danielwestheide.com/blog/2012/12/26/the-neophytes-guide-to-scala-part-6-error-handling-with-try.html" rel="nofollow noreferrer">http://danielwestheide.com/blog/2012/12/26/the-neophytes-guide-to-scala-part-6-error-handling-with-try.html</a></li>
  </ul>