title: Pattern to handle expected errors locally, rethrow unexpected errors
body: <p>Sometimes a certain bit of code will raise an error in an expected way, and it's most convenient to handle it locally rather than throw it to an error handling routine where it will get mixed up with other errors of the same type. Yet you don't want unexpected errors to be swallowed; you want them to be raised as usual. </p>
  
  <p>In the (slightly contrived) example below, the <code>FindInArray</code> function can raise different types of error. One of them, <code>ERR__ELEMENT_NOT_FOUND_IN_ARRAY</code>, is more or less expected and so I want to handle it locally. But other error numbers may also occur, and if so I want them to be dealt with by the error handling routine. </p>
  
  <p>I find that if I deal with some expected error numbers locally, I can't easily "rethrow" unexpected error numbers to be dealt with elsewhere. </p>
  
  <p>How do I segregate the expected errors I want to deal with locally, from unexpected errors to be dealt with in error handling routine (or elsewhere)?</p>
  
  <pre class="lang-vb prettyprint-override"><code>On Error GoTo ErrorHandler
  
  'Some code...
  
  'Here I want to trap a likely/expected error locally, because the same
  'error may occur elsewhere in the procedure but require different handling.
  On Error Resume Next
  personIndex = FindInArray(personName, personArray)
  If Err.Number = ERR__ELEMENT_NOT_FOUND_IN_ARRAY Then
      MsgBox "Name not found in person array. Using default person."
  Else
      'What if it's a different kind of error?
      ' .e.g. ERR__ARRAY_CONTAINS_TWO_PERSONS_WITH_SAME_NAME
      'I want to rethrow it, but can't because On Error Resume Next swallows it.
  End If
  On Error GoTo ErrorHandler 'back to normal
  'I can't rethrow it here either, because On Error Goto cleared the Err object.
  
  '-----------------------
  ErrorHandler:
  Select Case Err.Number
  Case ERR__ELEMENT_NOT_FOUND_IN_ARRAY
      'The error number doesn't give me enough info 
      'to know what to do with it here!
  Case ERR__ARRAY_CONTAINS_TWO_PERSONS_WITH_SAME_NAME
      'Existing code to deal with this error
  Case ...
  </code></pre>
  
  <p>I guess I could "save" the error Number, Source, Description, etc. in some other variable / object, and use those to raise an error after <code>On Error GoTo ErrorHandler 'back to normal</code>, (and in fact I have implemented this just to see) but that seems terribly inconvenient and clumsy. </p>