title: Returning values vs returning error codes?
body: <p><strong>This is a general programming question, not pertaining to any specific language.</strong></p>
  
  <p>A new programmer typically will write a method that calculates some value, then returns the value:</p>
  
  <pre><code>public Integer doSomething()
  {
     // Calculate something
     return something;
  }
  
  public void main()
  {
    Integer myValue = doSomething();
  }
  </code></pre>
  
  <p>But when an exception occurs during the calculation of <code>something</code>, what is the best way to handle the exception, especially when giving the user feedback? If you do a try/catch of the calculation of <code>something</code> and if an exception is caught, what do you return? Nothing was calculated, so do you return null? And once you return it (whatever it may be), do you need to do another try/catch in the parent method that checks to see if a valid value was returned? And if not, then make sure the user is given some feedback?</p>
  
  <p>I have heard arguments on both sides of the table about never returning values at all, but instead setting calculated values as pointers or global variables and instead returning only error codes from methods, and then (in the parent method) simply handling the error codes accordingly.</p>
  
  <p>Is there a best practice or approach to this? Are there any good resources that one could access to learn more about the best way to handle this?</p>
  
  <p><strong>UPDATE for Clarification</strong></p>
  
  <p>Consider the following code:</p>
  
  <pre><code>public void main()
  {
    int myValue = getMyValue();
  
    MyUIObject whatever = new MyUIObject();
    whatever.displayValue(myValue); // Display the value in the UI or something
  }
  
  public Integer getMyValue()
  {
    try
    {
      // Calculate some value
    } catch (exception e) {
      // ??
    }
    return value;
  }
  </code></pre>
  
  <p>I call the method to get some int value, then I return it. Back in <code>main()</code>, I do something with the value, like show it in the Log in this case. Usually I would display the value in the UI for the user.</p>
  
  <p>Anyways, if an exception is caught in <code>getMyValue()</code>, so does <code>value</code> get returned but it's null? What happens in <code>main()</code> then? Do I have to test if it's a valid value in <code>main()</code> as well?</p>
  
  <p>I need the program to handle the error accordingly and continue. Someone below suggested displaying the appropriate information in the UI from within the <code>getMyValue()</code> method. I see two potential issues:</p>
  
  <ol>
  <li>It seems like I would be mixing the business logic with (in this case) the logic for the UI.</li>
  <li>I would have to pass a reference of the <code>MyUIObject</code> to the <code>getMyValue()</code> or something so I could access it from within the function. In the above simple example that is no big deal, but if there is a BUNCH of UI elements that need to be updated or changed based off of what happens in <code>getMyValue()</code>, passing them all might be a bit much...</li>
  </ol>
  
  <p>I've read a bunch about the fundamentals of all of this but I can't seem to find a straight answer for the above situation. I appreciate any help or insight.</p>