Long jumps considered inexpensive. (John Rose on the relative cost of Exceptions)
blogs.oracle.com/jrose/entry/longjumps_consid...Posted Mar 24, 2014, 6:49 PM UTC
Posted Mar 24, 2014, 6:49 PM UTC
So if the main cost is creating it, would it be (very) beneficial to build and have available some exceptions as static fields? From his description and use case, the stacktrace is not important here at all.
For me, the take away from the article is that 99% of the time, the conventional wisdom about Java exceptions is true. That is: they are a heavy-weight construct and should be exclusively used to report exceptional circumstances.
However... there's also a 1% case where what you really want to do is abort a deeply nested series of calls for a non-Exceptional reason. Consider a recursive-descent parser that detects a parse error in a deeply nested function. You may be 20 or 30 levels deep in the call stack when you encounter a parse error that essentially renders the entire parse invalid. Somehow, that deeply nested function needs to tell the top level of the parser that the parse is invalid (which probably also includes information on the nature of the error). There are at least two ways to do this:
In cases like this, it's useful to think of Exceptions as control flow constructs. (But be sure to disable fillInStackTrace and give the Exception a name that indicates its unusual use case.)
Scala does this, FWIW. In Scala, a
returninside a lambda is translated into athrowandcatchby the compiler. The exception class overridesfillInStackTrace, avoiding the overhead. Good stuff.Scala also provides a trait (I think called
NoStackTrace) that encapsulates this behavior, so you can conveniently make more flow-control exceptions yourself.That's...awesome. I'm definitely excited for my online Scala course next month.
That's not awesome. That's a minor convenience. Wait'll you see what's really awesome about Scala.
I'm aware of some of it. Everything I have seen, read and heard about Scala sounds appealing. Even small stuff like a provided trait for rolling your own cheap long jump via throwables gets me excited, though. :P
It's a wonderful language. Flawed in some ways, but well worth the trouble.