Saturday, February 5, 2011

Why there are no checked exceptions in functional programming languages

Let's take Scala as an example. The problem with checked exceptions is best demonstrated by the map method on lists:
  def map[B](f: A => B): List[B]

How to annotate map with @throws? If map does not get a @throws annotation itself then presumably you cannot pass it any function that has a @throws. That would introduce cumbersome restrictions and distinctions for the ways in which map can be used. Things would be better if we could state somehow that map throws all exceptions that its function argument throws. There are some effect systems that can express this, but so far every notation I have seen is too heavy.

Martin Odersky