When to use multi-catch and when to use rethrow?
Asked Answered
D

3

5

I'm very uncertain about this two topics. I know that i should use multi-catch for exceptions which need to handle the same way. But for what purpose do i really need something like that.

private void something(String name) throws IOException, RemoteException {
    try {
        ...
    } catch (Exception ex) {

        ... // do something

        throw ex;
    }
}
Diaspore answered 17/4, 2017 at 12:29 Comment(2)
Using a generic exception is a bad practice though. See here.Sprung
Isn’t this question both unclear and opinion-based?Wren
L
3

You can do it if you consider for this method that any exception thrown during its execution should be handled in the same way and you want perform a task before letting propagate the exception to the client

For example, suppose you want to do a particular processing when the exception happens such as logging the information. So you catch it to do this task.
Nevertheless you consider that the caught exception is a problem and that logging it was not a "real" handling of the exception. So, you let it propagate by rethrowing it.

Lakitalaks answered 17/4, 2017 at 12:31 Comment(7)
Ok, thanks. But may i ask one more question? Let's assume an NullPointerException is thrown and cought by the catch. Will this Exception also be thrown to the calling methode, because in the throws-clause are only IO- and RemoteException mentioned? Greetings from GermanyDiaspore
You are welcome. Very interesting question. NullPointerException is a RuntimeException. This kind of exception doesn't need to be explicitly declared in the method declaration. You can throw it without catching it or declaring the method as throwing it. Contrary to IOException and RemoteException that are checked exceptions. These last derive from Exception only and not from RuntimeException.Lakitalaks
That's true...i didn't consider that. And what would happen if an IllegalArgumentException is thrown, for instance because i wanted to store a String in an int-variable? Would it be thrown to the calling method anyway although it isn't mentioned in the throws-clause?Diaspore
Of course, it will be thrown to the calling method but as it is a RuntimeException, the caller may decide to not handle it. When you want ensure that the exception be handled (thrown or caught), you should use a checked exception (... extends Exception). Don't hesitate to create own of them if it is suitable.Lakitalaks
That was clumsy of me. xD I forgot that it is a RuntimeException too. What i actually intended to ask was what would happen if a checked exception occurs in the try-block and is hand to the catch-block and then thrown to the calling method without mentioning it in the throws-clause. To cut a long story short i finally tested it and the answer is ( like you mentioned it before too) as follows: When i throw a checked exception, then regardless whether the exception is mentioned in the throws-clause the calling method then must handle it on its own. xD Thank you very much!Diaspore
Trying is often the best way to understand :) You are very welcome :)Lakitalaks
I consider ”log and rethrow“ a clear anti-pattern and a way to get your log filled with useless stuff that in the end may make it hard to find what you need among the garbage. The caller that catches and handles the exception in the end can much better decide whether and how it is to be logged. And with proper exception chaining it can log every detail if it needs to.Wren
D
2

ReThrow

Whenever you want to notify caller method about exception, you catch and rethrow exception.

Say some method callSomething() is calling your method something(). If any exception occurs inside something (), it will just catch exception, so application doesn't fail, and rethrow it to the callSomething() method. Then callSomething() will notify client about internal error.

Other example is, in MVC pattern, request submitted by client is served by some method from controller based on request mapping. Controller will call service, and service will interact with some method of dao. If some exception occurs in DAO, then DAO will rethrow exception to service, service will rethrow to controller, and it is controller which will notify client about error message. This is known as Exception propagation in java. An exception propagates from method to method, up the call stack, until it's caught.

Multi catch

If you want to perform same action for multiple types of exception, then you use multi catch.

Dysarthria answered 17/4, 2017 at 12:43 Comment(0)
N
2

You'll need rethrow in following situations

  1. You want to preprocess something before letting the exception leave the method. However, if you don't care about the type of the exception then preprocessing can be done in the finally block as well.
  2. You're trying to convert the checked exceptions into unchecked exception. In that case you'll be catching all the exceptions as catch(Exception ex) and rethrowing them as throw new RuntimeException(ex).
  3. You want your custom exception to be thrown instead of in built ones. So you'll catch all the exceptions and then create your own exception object preferably unchecked one and throw that. Many APIs do this, for example Spring converts untuitive JDBC exceptions to Spring custom exceptions.
  4. This one is kind of like preprocessing. You want to keep track of all the exceptions thrown by creating an ArrayList or something, so that at the end of a program with multiple steps you know which steps throw exceptions. I have seen this one being used in Talend generated Java code.
Nanceynanchang answered 17/4, 2017 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.