Are try-blocks expensive in time and space?

Terence Parr

Exceptions are essential to writing robust code and, fortunately, are extremely cheap until you actually throw an exception; the try costs you very little (usually the cost of marking the stack pointer). Exceptions are for exceptional behavior and so an expensive throw operation is usually ok as they are not executed very often.

Do not use exceptions for control-flow in your program.

On the other hand, I do one of exactly one legitimate use of exceptions for control-flow: backtracking algorithms in very large complicated programs (and I'd limit it to auto generated programs due to the "goto-like" behavior they cause). I have found that they don't affect parsing speed much when backtracking over large input streams if I'm smart about it (only backtracking when totally necessary).

About | Sitemap | Contact