Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use multi-catch and when to use rethrow?

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;
    }
}
like image 463
LaBlum Avatar asked Apr 17 '17 12:04

LaBlum


1 Answers

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.

like image 64
davidxxx Avatar answered Sep 21 '22 15:09

davidxxx