Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is an alternative to exceptions for flow control?
    primarykey
    data
    text
    <p>I inherited a java application that processes requests and throws an exception if it determines a request should be cancelled. Exceptions have been convenient for the previous developer because they are an easy way to exit out of a tree of logic that no longer applies (yes a goto) and it prints a stack trace to the log which is good info to have. It seems to be the consenus on forums and blogs that exceptions should not be used for flow control and over half of the requests processed are cancelled, so they're definitely not exceptional situations. One argument is performance, which doesn't apply because our exception-ful code has run fast enough for years. Another argument is that the goto-ness of it is not clear, which I agree with. My question is: What is the alternative. The only thing I can think of is to have every method return true if processing should continue or false if it shouldn't. This seems like it would hugely bloat my code changing this:</p> <pre><code>checkSomething(); checkSomethingElse(); </code></pre> <p>into this:</p> <pre><code>boolean continueProcessing = false; continueProcessing = checkSomething(); if (!continueProcessing) { return false; } continueProcessing = checkSomethingElse(); if (!continueProcessing) { return false; } </code></pre> <p>And then what if the method is supposed to return something? Any guidance would be great. I'd really like to observe whatever "best practices" are available.</p> <p>Update:</p> <p>Another bit I probably should have mentioned in the first place is that a request is cancelled more than 50% of the time and does not mean something didn't go right, it means the request wasn't needed after all.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload