In embedded programming, exceptions were traditionally not allowed, because the overhead of the stack unwinding you have to do was deemed an unacceptable variability when trying to maintain real-time performance. While smartphones could technically be considered real time platforms, they are powerful enough now where the old limitations of embedded systems don't really apply anymore. I just bring it up for the sake of thoroughness.
Exceptions are often supported in functional programming languages, but so rarely used that they may as well not be. One reason is lazy evaluation, which is done occasionally even in languages that are not lazy by default. Having a function that executes with a different stack than the place it was queued to execute makes it difficult to determine where to put your exception handler.
The other reason is first class functions allow for constructs like options and futures that give you the syntactic benefits of exceptions with more flexibility. In other words, the rest of the language is expressive enough that exceptions don't buy you anything.
I'm not familiar with Swift, but the little I've read about its error handling suggests they intended for error handling to follow more functional-style patterns. I've seen code examples with success
and failure
blocks that look very much like futures.
Here's an example using a Future
from this Scala tutorial:
val f: Future[List[String]] = future {
session.getRecentPosts
}
f onFailure {
case t => println("An error has occured: " + t.getMessage)
}
f onSuccess {
case posts => for (post <- posts) println(post)
}
You can see it has roughly the same structure as your example using exceptions. The future
block is like a try
. The onFailure
block is like an exception handler. In Scala, as in most functional languages, Future
is implemented completely using the language itself. It doesn't require any special syntax like exceptions do. That means you can define your own similar constructs. Maybe add a timeout
block, for example, or logging functionality.
Additionally, you can pass the future around, return it from the function, store it in a data structure, or whatever. It's a first-class value. You're not limited like exceptions which must be propagated straight up the stack.
Options solve the error handling problem in a slightly different way, which works better for some use cases. You're not stuck with just the one method.
Those are the sorts of things you "gain by losing exceptions."
panic
which is not quite the same. In addition what is said there, an exception is not much more than a sophisticated (but comfortable) way to perform aGOTO
, although nobody calls it that way, for obvious reasons. – JensG Oct 3 at 18:51