Tim Stewart
Tim Stewart

Reputation: 5500

Why am I not being warned about a non-exhaustive match?

Compiling this code does not result in any warnings about exhaustive pattern matches.

package example

object Hello extends App {

  class Problem extends Exception {
    override def getCause() : Throwable = new NotImplementedError("I'm not done")
  }

  handle(new Problem)

  def handle(ex: Exception): Unit = {
    println("handler called: %s".format(ex))
     val result = ex.getCause match {
       case null => println("null")
       case e : Exception => println("except")
       // no warning about the match not being exhaustive
       // case t : Throwable => println("throw")
     }
  }
}

When I run the code I get this stack trace:

scala.MatchError: scala.NotImplementedError: I'm not done (of class scala.NotImplementedError)
        at example.Hello$.handle(Hello.scala:13)
        at example.Hello$.delayedEndpoint$example$Hello$1(Hello.scala:9)
        at example.Hello$delayedInit$body.apply(Hello.scala:3)
        at scala.Function0.apply$mcV$sp(Function0.scala:39)
        at scala.Function0.apply$mcV$sp$(Function0.scala:39)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
        at scala.App.$anonfun$main$1(App.scala:76)
        at scala.App.$anonfun$main$1$adapted(App.scala:76)
        at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
        at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
        at scala.collection.AbstractIterable.foreach(Iterable.scala:926)
        at scala.App.main(App.scala:76)
        at scala.App.main$(App.scala:74)
        at example.Hello$.main(Hello.scala:3)
        at example.Hello.main(Hello.scala)

If I uncomment out the line case t : Throwable => println("throw"), the program prints throw.

Why isn't a warning printed to the console when I compile this code? Thanks!

Upvotes: 1

Views: 206

Answers (0)

Related Questions