This answer is taken here: http://www.javapractices.com/topic/TopicAction.do?Id=77
Data can be stored in various ways, for example: a relational database of text files on the Internet (for example, obtaining weather forecasts from a website). If the storage method changes, then low-level Exception objects thrown by the data access level can also change. For example, when a data store is moved from text files to a relational database, an IOException is replaced by a SQLException. To prevent such a change from spreading to higher levels, you can wrap such low-level exceptions in a general โdata exclusionโ wrapper designed solely to protect higher levels from such changes.
Now we will see an example ...
public class ResourceLoader { public loadResource(String resourceName) throws ResourceLoadException { Resource r; try { r = loadResourceFromDB(resourceName); } catch (SQLException e) { throw new ResourceLoadException("SQL Exception loading resource " + resourceName: " + e.toString()); } } }
The loadResource implementation uses exceptions quite well. Throwing a ResourceLoadException instead of a SQLException (or any other exceptions that the implementation performs), loadResource hides the implementation from the caller, which makes it easy to change the implementation without changing the call code. In addition, the exception thrown by loadResource () - ResourceLoadException, relates directly to the task it performs: loading the resource. Low-level exceptions SQLException and IOException are not directly related to the task that this method performs, and therefore are likely to be less useful to the caller. In addition, this package saves the original exception error message, so the user knows why the resource cannot be loaded (possibly due to a connection error or an incorrect username or password) and can take corrective actions.
source share