Note that there are some explanatory texts on larger screens.

plurals
  1. POUse continue or Checked Exceptions when checking and processing objects
    text
    copied!<p>I'm processing, let's say a list of "Document" objects. Before I record the processing of the document successful I first want to check a couple of things. Let's say, the file referring to the document should be present and something in the document should be present. Just two simple checks for the example but think about 8 more checks before I have successfully processed my document.</p> <p>What would have your preference?</p> <pre><code>for (Document document : List&lt;Document&gt; documents) { if (!fileIsPresent(document)) { doSomethingWithThisResult("File is not present"); continue; } if (!isSomethingInTheDocumentPresent(document)) { doSomethingWithThisResult("Something is not in the document"); continue; } doSomethingWithTheSucces(); } </code></pre> <p>Or</p> <pre><code>for (Document document : List&lt;Document&gt; documents) { try { fileIsPresent(document); isSomethingInTheDocumentPresent(document); doSomethingWithTheSucces(); } catch (ProcessingException e) { doSomethingWithTheExceptionalCase(e.getMessage()); } } public boolean fileIsPresent(Document document) throws ProcessingException { ... throw new ProcessingException("File is not present"); } public boolean isSomethingInTheDocumentPresent(Document document) throws ProcessingException { ... throw new ProcessingException("Something is not in the document"); } </code></pre> <p>What is more readable. What is best? Is there even a better approach of doing this (maybe using a design pattern of some sort)? </p> <p>As far as readability goes my preference currently is the Exception variant... </p> <p>What is yours?</p>
 

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