When to use multilova and when to use retrol?

I highly doubt these two topics. I know that I should use multi-catch for exceptions that should be handled the same way. But why do I really need something like that.

private void something(String name) throws IOException, RemoteException { try { ... } catch (Exception ex) { ... // do something throw ex; } } 
+5
source share
3 answers

You can do this if you consider for this method that any exception that occurred during its execution should be handled the same way and you want to complete the task before allowing the exception to be distributed to the client

For example, suppose you want to perform certain processing when an exception occurs, such as logging. Therefore, you will catch him to complete this task.
However, you think that the caught exception is a problem and that registration was not a β€œreal” exception handling. That way you let it spread by dragging it.

+3
source

Rethrow

Whenever you want to notify the caller's method of an exception, you get a catch and rethrow exception.

Let's say some callSomething () method calls your something () method. If any exception occurs inside something (), it will simply catch the exception, so the application does not interrupt and reinstalls it on the callSomething () method. Then callSomething () will notify the client of an internal error.

Another example is that in the MVC template, the request sent by the client is served by some method from the controller based on query matching. The controller will call the service and the service will interact with some dao method. If some kind of exception occurs in the DAO, then the DAO cancels the exception for the service, the service will be redirected to the controller, and this is the controller that will notify the client of the error. This is known as throwing exceptions in java. An exception extends from method to method, to the call stack, until it is caught.

Multi catch

If you want to perform the same action for several types of exceptions, then you use multiple catch.

+3
source

You will need to retrony in the following situations

  • You want to pre-process something before letting the exception leave this method. However, if you do not care about the type of exception, then preprocessing can also be performed in the finally block.
  • You are trying to convert checked exceptions to exception without exception. In this case, you will catch all exceptions as catch(Exception ex) and invert them as throw new RuntimeException(ex) .
  • You want your custom exception to be selected instead of the built-in ones. This way you will catch all the exceptions, and then create your own exception object, it is advisable that it is not marked, and drop it. Many APIs do this, for example Spring converts non-exclusive JDBC exceptions to Spring custom exceptions.
  • This is like preprocessing. You want to keep track of all the exceptions thrown with ArrayList or something else, so at the end of the program with a few steps, you know which steps the exceptions execute. I saw that this one is used in Talend generated Java code.
+2
source

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


All Articles