title: Dealing with inaccessible exception types
body: <p>I'm working with a third-party library that has some very specific exception types for different scenarios. I'd like to use them like this (a somewhat simplified version):</p>
  
  <pre><code>try
  {...}
  catch (IllegalFooOperationException e)
  {
      //handle an illegal foo
  }
  catch (InvalidBarValueException e)
  {
      //handles very differently from the illegal foo.
  }
  </code></pre>
  
  <p>Unfortunately, <code>IllegalFooOperationException</code> and <code>InvalidBarValueException</code> are <em>not</em> public classes: they are package-scoped. They inherit from a much more generic public <code>GenericThirdPartyAPIException</code> class.</p>
  
  <p>Right now, this sort-of works:</p>
  
  <pre><code>catch (GenericThirdPartyAPIException e)
  {
      String rootExceptionTypeName = e.getClass().getName();
      if (rootExceptionTypeName.equals("someAPI.IllegalFooOperationException"))
      {
          //handle illegal foo
      }
      else if (rootExceptionTypeName.equals("someAPI.InvalidBarValueException"))
      {
          ...
      }
  }
  </code></pre>
  
  <p>But this just doesn't feel right. One potential problem is that if the packages or class names change in the future in this library, this code won't be able to find the right exception class at runtime. I don't like relying on figuring out an object's class at runtime if I don't have to.</p>
  
  <p>I also thought about trying to parse the exception message directly, but there some of the exceptions I'm interested in (not all are shown here) <em>might</em> have the same message, but be of a different type.</p>
  
  <p>Is there a better way to handle this situation?</p>