Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

As a java programmer, I have always been critical of Unchecked Exceptions. Mostly programmers use it as an en-route to coding easiness only to create trouble later. Also the programs (though untidy) with checked exceptions are much robust compared to unchecked counterparts.

Surprisingly in Scala, there is nothing called Checked Exceptions. All the Java checked and unchecked are unchecked in Scala.

What is the motivation behind this decision? For me it opens wide range of problems when using any external code. And if by chance the documentation is poor, it results in KILL.

share|improve this question
5  
As a Java programmer, I have always been critical of checked exceptions. Untidy code is never robust. –  Michael Borgwardt Nov 30 '12 at 8:11

2 Answers 2

up vote 13 down vote accepted

Checked exceptions are mostly considered a failure. Note that no languages created after Java adopted them. See http://www.artima.com/intv/handcuffs2.html, http://googletesting.blogspot.ru/2009/09/checked-exceptions-i-love-you-but-you.html, http://www.mindview.net/Etc/Discussions/CheckedExceptions, etc.

In particular, they are uncomposable (except by reverting to throws Exception).

In Scala you have a better option: using algebraic types for return values such as Option[T], Either[Exception, T], your own type when you want the user to handle specific cases (e.g. instead of

def foo: Int // throws FileNotFoundException, IllegalStateException

you have

sealed trait FooResult
case class Success(value: Int) extends FooResult
case class FileNotFound(file: File) extends FooResult
case object IllegalState extends FooResult

def foo: FooResult

and the consumer now is required to handle all results)

For dealing with external code which does throw exceptions, you have scala.util.control.exception or scala.util.Try (starting with Scala 2.10).

share|improve this answer

If you want to gain efficiency, you have to give up.. precision/control <-- I need a better word for that.

Scala is located towards the top as far as abstraction goes. If one of Scala's goals is getting rid of annoying boilerplate code, then an obvious place to look at is Java's exception handling. If you want to write fast code in java, just keep throwing your checked exceptions up until they hit main() and effectively become unchecked.

I don't know if I'm getting at exactly what you're asking but this is the most obvious reason in my opinion.

Well, I did a little looking and someone has written about the tragedy of check exceptions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.