title: Adding error handling to a function that loads data (but also needs to use default values sometimes)
body: <p>I'm trying to find a non-clunky way to introduce error-handling into function that loads data from a data source. The snag is that the DataSource may (or may not) contain invalid data, and that in the event that a value fails to be parsed, depending on the DataSource's 'revision' either a default value should be used or an error should be generated:</p>
  
  <pre><code>class Foo {
  
      int value_1;
      int value_2;
      //...
  
      void loadDataFromSource(DataSource source)
      {
          int revision = source.getRevision();
          value_1 = source.parseValue(1);
          value_2 = source.parseValue(2);
          //...
      }
  }
  </code></pre>
  
  <p>The function "parseValue()" currently returns a default value if it fails to parse, but as mentioned above, error handling needs to be introduced. I could have "parseValue()" throw exceptions and handle them (depending on DataSource revision), e.g.</p>
  
  <pre><code>void loadDataFromSource_NewRevision(DataSource source)
  {
      //Exceptions thrown by parseValue() are handled by the caller of this function
      value_1 = source.parseValue(1);
      value_2 = source.parseValue(2);
      //...
  }
  
  void loadDataFromSource_OldRevision(DataSource source)
  {
      //Exceptions thrown by parseValue() are discarded and default values are used
      try { value_1 = source.parseValue(1); }
      catch(...) { value_1 = 42; }
      try { value_2 = source.parseValue(2); }
      catch(...) { value_2 = -7; }
      //...
  }
  </code></pre>
  
  <p>But the code duplication and the entire set-up feels just bad. Same goes for using some kind of flag to tell parseValue() to throw/not-throw exceptions.</p>
  
  <pre><code>void loadDataFromSource(DataSource source)
  {
      int revision = source.getRevision();
      bool useDefaultValueOnError = revision &lt; 10;
      //Exceptions thrown by parseValue() - if any - are handled by the caller of this function
      value_1 = source.parseValue(1, useDefaultValueOnError, 42);
      value_2 = source.parseValue(2, useDefaultValueOnError, -7);
      //...
  }
  </code></pre>
  
  <p>I'd like to avoid the code duplication, but the 'flag' option looks equally unappealing. Does anyone know of a better way to do this?</p>
  
  <p>Edit / Small update:
  I managed to reduce the number of values, that we need to use a default value for (if it fails to parse in the old revision of the data source) to a mere handful. For those I created a second "parseValueWithDefault" function with a parameter and a "use default-on-error" flag which invokes the original parse function, catches the error and either returns the default or rethrows the exception. Not perfect, but I feel that was the least bad option at this time.</p>
  
  <pre><code>int parseValueWithDefault(int valueId, int defaultValue, bool useDefaultOnError)
  {
      try 
      {
          return source.parseValue(valueId);
      }
      catch (Exception&amp; e)
      {
          if (useDefaultOnError)
              return defaultValue;
          throw;
      }
  }
  </code></pre>