What is exception packaging in Java?

What is Exception wrapping in Java? How is this useful when handling exceptions? How does it differ from exception propagation?

+5
source share
3 answers

Exception wrapping is when you catch an exception, wrap it in another exception and throw that exception.

Here is an example:

  try{ dao.readPerson(); } catch (SQLException sqlException) { throw new MyException("error text", sqlException); } 

Source: http://tutorials.jenkov.com/java-exception-handling/exception-wrapping.html

On the other hand

Exception Propagation : An exception is first thrown from the top of the stack, and if it is not caught, it drops the call stack to the previous method, if it is not caught, the exception is dropped again to the previous method, etc. until they are caught or until they reach the lowest level of the call stack.

Source: http://www.javatpoint.com/exception-propagation

+4
source

Disposal will be to include the checked exception in the runtime exception, or vice versa.

Or it could just be naming. Let's say you catch a SQLException at some point in your code, but you can reason it because the user is not logged in. Then you can catch it and throw your own NotLoggedInException.

0
source

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.

0
source

Source: https://habr.com/ru/post/1215086/


All Articles