0

I have few similar methods and there calls as follows :

methodThrowingException() throws NullPointerException, InterruptedException, TimeoutException {
 <method logic>
}

Calling class :

 try{
    methodThrowingExceptions();
    <some other logic>
    }
    catch (NullPointerException npx) {
        npx.printStackTrace();
        log more details...
    }
    catch (InterruptedException inx) {
        inx.printStackTrace();
        log more details...
    }
    catch (TimeoutException tox) {
        tox.printStackTrace();
        log more details..
    }
  1. How (if) can I put all of these three in one Custom Exception?
  2. Other than (1) is there a way to optimise the code so that I need not write the entire same statements for multiple methods?
Naman
  • 27,789
  • 26
  • 218
  • 353

1 Answers1

1

Since Java 7, you can use a multi-catch block:

catch (NullPointerException | InterruptedException | TimeoutException e) {
    e.printStackTrace();
    log more details...
}

That said, you should never catch NullPointerException: that is a bug, and if it happens, the exception should bubble up. You can't reasonably expect a NPE to happen.

In addition, doing the same thing for an InterruptedException as for the other exceptions is also very dubious. When catching an InterruptedException, you should reset the interrupted flag on the current thread, and stop what you're doing ASAP.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanx for the answer. Part 2 is solved by this. Could you suggest something for Part 1. – Naman Dec 28 '15 at 01:51
  • I don't understand what you ask in part 1. Are you asking how to throw a rethrow a single custom exception when catching any of those 3 exceptions? – JB Nizet Dec 28 '15 at 06:25
  • kind of a custom exception to catch any of these 3 exceptions in one and perform different operations for each type – Naman Dec 28 '15 at 07:01
  • If you want to perform 3 different operations based on the type of the exception, having 3 different catch blocks is a good thing. How would you make 3 different things with a single catch block? The only common supertype is Exception, and you really don't want to catch (Exception), as that would also catch any other exception that you shouldn't catch. – JB Nizet Dec 28 '15 at 07:05
  • true. `instanceof` does help in the case but I want to create a Custom Exception class for the listed exceptions and then catch that `CustomException` in my original class. – Naman Dec 28 '15 at 07:12
  • That makes no sense. If you call a method that throws an InterruptedException, it throws an InterruptedException, and that's the exception you need to catch. Catching a different, custom exception won't catch the InterruptedException. Using instanceof is ugly. Having separate catch blocks is precisely useful to *avoid* instanceof checks. – JB Nizet Dec 28 '15 at 07:16
  • so in short i should be practicing the way it has been previously without catching null pointer ...is it?? – Naman Dec 28 '15 at 09:18
  • 1
    It's very unclear. Your question started by asking how you could avoid repeating the same code in 3 different catch blocks doing the same thing, and then it ended up asking how you could do different things based on the type of the exception. If it's the former, use a multi-catch. If it's the latter, use a catch block per exception. – JB Nizet Dec 28 '15 at 09:24
  • modified the statement, maybe this could clear by intentions – Naman Dec 29 '15 at 12:50
  • That's yet another, completely different question. Mark this one as answered, and ask another, separate question. – JB Nizet Dec 29 '15 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99203/discussion-between-nullpointer-and-jb-nizet). – Naman Dec 29 '15 at 12:56