Many operations on the low level of the framework perform rigorous tests of passed parameters and throw appropriate exceptions when faced with incorrect parameters or in other exceptional cases. Question at hand is whether these exceptions should extend RunTimeException or Exception.
Fоr instance I have a method which performs some low-level operations, say addition:
public void add(Integer a, Integer b)
throws OperationException
{
}
Between a high level operation and the add method there is a chain of many methods. If I don't declare the OperationException as RuntimeException, all those methods in the chain have to specify "throws OperationException". And some of these methods in the chain, frankly, have no idea what an OperationException is and the declaration of the fact that it throws it, looks out of place, just because somewhere on the lower levels there might be an OperationException. By the time we get to the upper levels of the library, we will accumulate a significant number of these low-level exceptions which we will be forcing a programmer to handle whenever he calls a high-level method.
Consultation with several luminaries confirms a very simple rule of thumb: "All exceptions should be regarded as Runtime, unless a user of the API can do anything intelligent regarding that specific exception"
Another example of a low-level method which could possibly throw a MissingResourceException. Here if we were not to re-throw a RuntimeException all of higher level methods would have to declare throws MissingResourceException
public static ResourceBundle getDefaultResourceBundle(Class clazz)
{
try
{
return ResourceBundle.getBundle(getResourceBundleName(clazz));
}
catch (MissingResourceException e)
{
throw new RuntimeException(MessageFormat.format(pp.getString("MISSING_RESOURCE_BUNDLE"), new Object[] {getResourceBundleName(clazz)}), e);
}
}