Consider the following Scala code:
case class Data[T](value: Option[T]) { def get: T = try { doGet } catch { case e: Exception => throw new IllegalArgumentException } def doGet: T = value match { case Some(v) => v case None => ().asInstanceOf[T] } } Data[Unit](None).get Data[Integer](None).get // which exception is thrown here? [spoiler] It is a ClassCastException; who can explain why it is not caught and replaced by an IllegalArgumentException?
PS: To preempt any questions on why I would want to do this: this is a simplified version of some code that uses json4s to parse some string into an Option[T]; if the parsing fails None is returned, which is OK if T was Unit and not OK if T is some other type.