0

I have following code example:

 private boolean openThroughCommPort(IProgressMonitor monitor, int portNum)
       throws InterruptedException, PortInUseException, IOException,
       UnsupportedCommOperationException, TooManyListenersException,
       UnsupportedVehicleException, InnerCanceledException {
        ...
       }

I don't sure that it is good way - throws many many exceptions in method signature.

Maybe you can tell me best practies for this case?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • You can always launch Exception. All exceptions inherits from it – Carlos Landeras Oct 16 '13 at 07:38
  • Is knowing which exception has been thrown important for the code that calls that method? – assylias Oct 16 '13 at 07:38
  • @CarlosLanderas: That's a really bad idea though - it reduces the effective documentation. – Jon Skeet Oct 16 '13 at 07:39
  • Agree it is a bad idea. It depends on how that code needs to be handled and how important is to catch exceptions in that scope. But the question seemed to say, I dont like so many lines, so ugly :) – Carlos Landeras Oct 16 '13 at 07:40
  • Even if there's a lot of exceptions, it's not *that* disturbing. The abstraction level of the exceptions seems to be consistent with the abstraction level of the method. – zakinster Oct 16 '13 at 07:40

4 Answers4

6

First of all if a method throws that many exceptions it means that the method is doing too many things. You should think of distributing the work to separate methods.

Secondly if it is important for the caller (the caller is at same abstraction level) to know which exact exception occurred and take particular actions on it then it makes more sense to throw individual exceptions and not wrap it. Also the point made by @Jon Skeet is true that it will also reduce possibility of effective documentation.

If the caller does not need to know the exact cause, if the abstraction level is different then it would make sense to wrap the exception in your own custom exception which is inline with the caller's abstraction level.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

I would recommend to create your own wrapper Exception and use try/catch in your method. Narendra Pathai is completely right that you should only do that if the caller does not need to decide who to proceed based on which exception was originally thrown.

You can wrap your exceptions like that:

try{ 
   //do something 
} catch(Exception e){ 
   throw new WrapperException(e);
}
mithrandir
  • 738
  • 1
  • 6
  • 17
0

It is always better to handle specific exceptions rather than generalizing it. There is nothing wrong in throwing so many exceptions. This would better help in documenting things also.

Raghav
  • 6,893
  • 3
  • 19
  • 28
0

Do not wrap or swallow the InterruptedException, or your application will behave incorrectly in a threaded context. Just a couple of links for reference, be sure to handle it in a proper way:
http://www.ibm.com/developerworks/java/library/j-jtp05236/
Java Thread Sleep and Interrupted Exception

Calling methods should deal with exceptions or let them flow up. You can add a short description on method's Javadoc.

Community
  • 1
  • 1
Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64