title: Scala Try and different Failure cases
body: <p>This code return "I've failed" as excepted.</p>
  
  <pre><code>  Try({
      throw new Exception()
    }) match {
      case Failure(e) =&gt; println("I've failed")
      case Success(s) =&gt; println("I've succeeded")
    }
  </code></pre>
  
  <p>But what if I have different Exception, such as:</p>
  
  <pre><code>  Try({
      if(someBoolean) {
        throw new DifferentException()
      } else {
        throw new Exception()
      }
    }) match {
      case Failure(e) =&gt; println("I've failed")
      case Success(s) =&gt; println("I've succeeded")
    }
  </code></pre>
  
  <p>How do I change the match statement so that we have a different case for <code>Exception</code> and <code>DifferentException</code>?</p>
  
  <p>I understand I can do this:</p>
  
  <pre><code>  Try({
      if(true) {
        throw new DifferentException()
      } else {
        throw new Exception()
      }
    }) match {
      case Failure(e) if e.isInstanceOf[DifferentException] =&gt; println("I've failed differently")
      case Failure(e) if e.isInstanceOf[Exception]          =&gt; println("I've failed")
      case Success(s) =&gt; println("I've succeeded")
    }
  </code></pre>
  
  <p>Is this the best, i.e least verbose, way to achieve such?</p>
  
  <p>[Edit]</p>
  
  <p>Thank you to Ashesh. The least verbose, for me, was this:</p>
  
  <pre><code>case class DifferentException() extends Exception() {}
  
  Try({
    if(someBoolean) { 
      throw new DifferentException
    } else { 
      throw new Exception
    } 
  }) match { 
    case Failure(DifferentException()) =&gt; println("I've failed differently")
    case Failure(e)                    =&gt; println("I've failed")
    case Success(s)                    =&gt; println("I've succeeded")
  }
  </code></pre>