The first example is usually seen as the best approach.
You should also not consider a MyBusinessException to wrap a NumberFormatException , but most likely a NumberFormatException is the cause of a MyBusinessException .
Exceptions must be appropriate for the interface. The subscriber of your interface does not need to know or describe the implementation details. If a NumberFormatException really makes sense as a type of error when calling exampleOneException , it should be thrown into a more appropriate exception.
A more specific example, as a rule, includes various implementations in which interface users are not required to be required for implementation specifics (which may not even be known at compile time).
interface MyRepository { Object read(int id) throws ObjectNotFoundException; }
Because an interface declares the types of errors that it can return, clients of that interface can be consistent and rational. By adding code to handle FileNotFoundException and SQLException , then the real implementation may be either or not fantastic.
Consider whether there were several places in the FileRepository implementation that could throw a FileNotFoundException . Does this mean that each of them implies that the object was not found?
When considering the second variant of exampleTwoException it is important to understand that your catch effectively states that the consequences of any error have been mitigated. There are times when checked exceptions are reasonably ignored, but it is much more likely that simple
try { ... } catch (SomeException ex) { log.error("caught some exception", ex); }
it is actually the result of the developer not taking into account the consequences of the exception, or the code must include FIXME .
This is even more true if you see catch (Exception ex) or incompatible catch (Throwable ex) .
And finally, do you want to be the person who could dig through the application, finding all the places where you need to add another catch block to handle the new implementation? Perhaps the reason is catch (Exception ex) ...
Always throw exceptions corresponding to abstractions