When a too general exception occurs in the library (as it seems you are working with an IOException here), you unfortunately do not have a lot of good options - essentially this is a bad design on the library part, Ideally you should file an error message or send a transfer request for the intruder library, but this may not be possible or possible.
However, all is not lost. First of all, it is possible that the library actually gives you more information than you think by including the main reason in the general IOException with Throwable.getCause() . This is a reliable way to provide more detailed debugging information when exposing a single Exception for normal processing. If this library follows this practice, you can do something like:
catch(IOException e) { Throwable cause = e.getCause(); if(cause != null && cause instanceof URISyntaxException) { System.err.println("Bad URI"); } else { System.err.println("Other IOException"); } }
Of course, the twist of what some Googling offers is a URISyntaxException in an IOException , it's not a terribly good design (an invalid URI, of course, not an I / O problem), so I wonβt be surprised if the library just calls something like throw new IOException(e.getMessage()) and discards the original, that is, unfortunately, all you need is an exception message.
As you noted, parsing exception messages is not a good idea. Ideally, you should wrap this bad behavior in a helper method so that you can easily reorganize it after the library has been improved and determine if something unexpected happened and turned to it. Consider:
public void doOperation(arguments) throws IOException, URISyntaxException { try { BadLibrary.doOperation(arguments); } catch(IOException e) { if(e.getMessage().startsWith("Illegal character")) { throw new URISyntaxException("Uknown", e.getMessage()); } else if(e.getMessage().startsWith("Stream closed")) { throw e; } else {
With this compartmentalization, you only need to do this unpleasant check in one place, and you can be sure that you will find unexpected changes in the base library, instead of silently ignoring them.