Back in the late 90's I worked quite a bit with a code base that used exceptions as flow control. It implemented a finite state machine to drive telephony applications. Lately I am reminded of those days because I've been doing MVC web apps.

They both have Controllers that decide where to go next and supply the data to that destination logic. User actions from the domain of an old-school telephone, like DTMF tones, became parameters to action methods, but instead of returning something like a ViewResult, they threw a StateTransitionException.

I think the main difference was that action methods were void functions. I don't remember all the things I did with this fact but I've been hesitant to even go down the road of remembering because since that job, like 15 years ago, I never saw this in production code at any other job. I assumed this was a sign that it was a so-called anti-pattern.

Is this the case, and if why?

Update: when I asked the question, I already had @MasonWheeler's answer in mind so I went with the answer that added to my knowledge the most.. I think his is a sound answer as well.

Your answer makes it sound as though exceptions are bad for all circumstances, while the quesion is focused on exceptions as flow control.
–
whatsisnameMar 4 '13 at 22:46

6

@MasonWheeler The difference is that for/while loops contain their flow control changes clearly and make the code easy to read. If you see a for statement in your code, you don't have to try to figure out which file contains the end of the loop. Goto's aren't bad because some god said they were, they are bad simply because they are harder to follow than the looping constructs. Exceptions are similar, not impossible but hard enough that they can confuse things.
–
Bill KMar 5 '13 at 0:02

9

@BillK, then argue that, and don't make over simplistic statements about how exceptions are gotos.
–
Winston EwertMar 5 '13 at 0:14

Okay but seriously, what is up with server-side and app devs burying errors with empty catch statements in the JavaScript? It's a nasty phenomon that has cost me a lot of time and I don't know how to ask without ranting. Errors are your friend.
–
Erik ReppenMar 5 '13 at 4:43

The use case that exceptions were designed for is "I just encountered a situation that I cannot deal with properly at this point, because I don't have enough context to handle it, but the routine that called me (or something further up the call stack) ought to know how to handle it."

The secondary use case is "I just encountered a serious error, and right now getting out of this control flow to prevent data corruption or other damage is more important than trying to continue onward."

If you're not using exceptions for one of these two reasons, there's probably a better way to do it.

This doesn't answer the question though. What they were designed for is irrelevant; the only thing that is relevant is why using them for control flow is bad which is a topic you didn't touch. As an example, C++ templates were designed for one thing but are perfectly fine to be used for metaprogramming, an use the designers never anticipated of.
–
Andreas BoniniMar 5 '13 at 1:26

1

@Krelp: The designers never anticipated a lot of things, such as ending up with a Turing-complete template system by accident! C++ templates are hardly a good example to use here.
–
Mason WheelerMar 5 '13 at 2:33

5

@Krelp - C++ templates are NOT 'perfectly fine' for metaprogramming. They are a nightmare until you get them right, and then they tend toward write-only code if you aren't a template-genius. You might want to pick a better example.
–
Michael KohneMar 5 '13 at 2:39

Exceptions are as powerful as Continuations and GOTO. They are a universal control flow construct.

In some languages, they are the only universal control flow construct. JavaScript, for example, has neither Continuations nor GOTO, it doesn't even have Proper Tail Calls. So, if you want to implement sophisticated control flow in JavaScript, you have to use Exceptions.

The Microsoft Volta project was a (now discontinued) research project to compile arbitrary .NET code to JavaScript. .NET has Exceptions whose semantics don't exactly map to JavaScript's, but more importantly, it has Threads, and you have to map those somehow to JavaScript. Volta did this by implementing Volta Continuations using JavaScript Exceptions and then implement all .NET control flow constructs in terms of Volta Continuations. They had to use Exceptions as control flow, because there is no other control flow construct powerful enough.

You mentioned State Machines. SMs are trivial to implement with Proper Tail Calls: every state is a subroutine, every state transition is a subroutine call. SMs can also easily be implemented with GOTO or Coroutines or Continuations. However, Java doesn't have any of those four, but it does have Exceptions. So, it is perfectly acceptable to use those as control flow. (Well, actually, the correct choice would probably be to use a language with the proper control flow construct, but sometimes you may be stuck with Java.)

If only we'd had proper tail-call recursion, maybe we could have dominated client-side web development with JavaScript and then followed up by spreading to the server-side and to mobile as the ultimate pan-platform solution like wildfire. Alas, but it was not so. Damn our insufficient first-class functions, closures and event-driven paradigm. What we really needed was the real thing.
–
Erik ReppenMar 5 '13 at 3:50

2

@ErikReppen: I realize that you're just being sarcastic, but . . . I really don't think the fact that we "have dominated client-side web development with JavaScript" has anything to do with the language's features. It has a monopoly in that market, so has been able to get away with a lot of problems that cannot be written off with sarcasm.
–
ruakhMar 5 '13 at 6:51

1

Many languages have neither gotos (Goto considered harmful!), coroutines or continuations and implement perfectly valid flow control simply using ifs , whiles and function calls (or gosubs!). Most of these can in fact be used to emulate each other, just as a continuation can be used to represent all of the above. (For instance you can use a while to perform an if, even if its a stinky way to code). So no exceptions are NOT necessary to perform advanced flow control.
–
ShayneNov 14 '14 at 3:05

1

Well yes you might be right about if. "For" in its C style form however can be used as an awkwardly stated while, and a while can then be used coupled with an if to implement a finite state machine that can then emulate all other flow control forms. Again, stinky way to code, but yeah. And again, goto considered harmful. (And I'd argue, so too with using exceptions in non exceptional circumstances). Keep in mind there are perfectly valid, and very powerful languages that provide neither goto nor exceptions and work just fine.
–
ShayneNov 19 '14 at 4:38

1

I'm curios about how exceptions can be used to implement continuations ..
–
hasenJan 11 at 8:50

It's completely possible to handle error conditions without the use of exceptions. Some languages, most notably C, don't even have exceptions, and people still manage to create quite complex applications with it. The reason exceptions are useful is they allow you to succinctly specify two essentially independent control flows in the same code: one if an error occurs and one if it doesn't. Without them, you end up with code all over the place that looks like this:

Or equivalent in your language, like returning a tuple with one value as an error status, etc. Often people who point out how "expensive" exception handling is, neglect all the extra if statements like above that you are required to add if you don't use exceptions.

While this pattern happens to occur most often when handling errors or other "exceptional conditions," in my opinion if you start seeing boilerplate code like this in other circumstances, you have a pretty good argument for using exceptions. Depending on the situation and implementation, I can see exceptions being used validly in a state machine, because you have two orthogonal control flows: one that's changing the state and one for the events that occur within the states.

However, those situations are rare, and if you're going to make an exception (pun intended) to the rule, you had better be prepared to show its superiority to other solutions. A deviation without such justification is rightly called an anti-pattern.

As long as those if statements only fail on 'exceptional' circumstances the branch prediction logic of modern CPUs make their cost negligible. This is one place where macros can actually help, as long as you're careful and don't try to do too much in them.
–
JamesMar 5 '13 at 0:25

If you have to terminate calculation somewhere in a deeply nested statement and go to some common continuation code, this particular path of execution can very probably be factored out as a function, with return in place of goto.
–
9000Mar 5 '13 at 0:40

1

@9000 I was about to comment exactly the same thing ... Keep try: blocks to 1 or 2 lines please, never try: # Do lots of stuff.
–
wimMar 5 '13 at 3:57

@9000, in some cases, sure. But then you lose access to local variables, and you move your code yet-another-place, when the process is a coherent, linear process.
–
gahooaMar 14 '13 at 5:45

1

@gahooa: I used to think like you. It was a sign of poor structure of my code. When I put more thought in it, I noticed that local contexts can be untangled and the whole mess made into short functions with few parameters, few lines of code, and very precise meaning. I never looked back.
–
9000Mar 14 '13 at 5:52

Using exceptions for control flow is generally considered an anti-pattern, but there are exceptions (no pun intended).

It has been said a thousand times, that exceptions are meant for exceptional conditions. A broken database connection is an exceptional condition. A user entering letters in an input field that should only allow numbers is not.

A bug in your software that causes a function to be called with illegal arguments, e.g. null where not allows, is an exceptional condition.

By using exceptions for something that is not exceptional, you are using inappropriate abstractions for the problem you are trying to solve.

But there can also be a performance penalty. Some languages have more or less efficient exception handling implementation, so if your language of choice does not have efficient exception handling, it can be very costly, performance-wise*.

But other languages, for example Ruby, have an exception-like syntax for control flow. Exceptional situations are handled by the raise/rescue operators. But you can use throw/catch for exception-like control flow constructs**.

So, although exceptions are generally not used for control flow, your language of choice may have other idioms.

* En example of a performance costly use of exceptions: I was once set to optimize a poorly performing ASP.NET Web Form application. It turned out, that the rendering of a large table was calling int.Parse() on approx. a thousand empty strings on an average page, resulting in approx. a thousand exceptions being handled. By replacing the code with int.TryParse() I shaved off one second! For every single page request!

** This can be very confusing for a programmer coming to Ruby from other languages, as both throw and catch are keywords associated with exceptions in many other languages.

As others have mentioned numerously, (e.g. in this Stack Overflow question), the principle of least astonishment will forbid that you use exceptions excessively for control flow only purposes. On the other hand, no rule is 100% correct, and there are always those cases where an exception is "just the right tool" - much like goto itself, by the way, which ships in the form of break and continue in languages like Java, which are often the perfect way to jump out of heavily nested loops, which aren't always avoidable.

The following blog post explains a rather complex but also rather interesting use-case for a non-localControlFlowException:

If we explicitly extracted the bind values from the query AST to count them every time, we'd waste valuable CPU cycles for those 99.9% of the queries that don't suffer from this problem.

Some logic is available only indirectly via an API that we want to execute only "partially". The UpdatableRecord.store() method generates an INSERT or UPDATE statement, depending on the Record's internal flags. From the "outside", we don't know what kind of logic is contained in store() (e.g. optimistic locking, event listener handling, etc.) so we don't want to repeat that logic when we store several records in a batch statement, where we'd like to have store() only generate the SQL statement, not actually execute it. Example:

// Pseudo-code attaching a "handler" that will
// prevent query execution and throw exceptions
// instead:
context.attachQueryCollector();
// Collect the SQL for every store operation
for (int i = 0; i < records.length; i++) {
try {
records[i].store();
}
// The attached handler will result in this
// exception being thrown rather than actually
// storing records to the database
catch (QueryCollectorException e) {
// The exception is thrown after the rendered
// SQL statement is available
queries.add(e.query());
}
}

If we had externalised the store() logic into "re-usable" API that can be customised to optionally not execute the SQL, we'd be looking into creating a rather hard to maintain, hardly re-usable API.

Conclusion

In essence, our usage of these non-local gotos is just along the lines of what Mason Wheeler said in his answer:

"I just encountered a situation that I cannot deal with properly at this point, because I don't have enough context to handle it, but the routine that called me (or something further up the call stack) ought to know how to handle it."

Both usages of ControlFlowExceptions were rather easy to implement compared to their alternatives, allowing us to reuse a wide range of logic without refactoring it out of the relevant internals.

But the feeling of this being a bit of a surprise to future maintainers remains. The code feels rather delicate and while it was the right choice in this case, we'd always prefer not to use exceptions for local control flow, where it is easy to avoid using ordinary branching through if - else.

The algorithm searches recursively till something is found. So comming back from recursion one has to check the result for being found, and return then, otherwise continue. And that repeatedly comming back from some recursion depth.

Besides needing an extra boolean found (to be packed in a class, where otherwise maybe only an int would have been returned), and for the recursion depth the same postlude happens.

Such an unwinding of a call stack is just what an exception is for. So it seems to me a non-goto-like, more immediate and appropriate means of coding. Not needed, rare usage, maybe bad style, but to-the-point. Comparable with the Prolog cut operation.