1

Lets take an example.

public String myMethod(String s) throws ExceptionA, ExceptionB {

} 

Is it bad to have this kind of multiple exception throwing from a method. i.e. do we need to simplify that as below using a wrapper?

public String myMethod(String s) throws ExceptionC{
    try{
    }catch(ExceptionA | ExceptionB e){
        throw new ExceptionC(e);
    }
}

If so, how can I find the root cause exception in higher level classes to log, when we bubble up the exception to the top?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • Instead of just `throw`ing an exception; always `try` the code and `catch` if there is an exception. – stuck Aug 04 '16 at 15:30
  • I like to think of it this way: Throws makes your life and code cleaner, use it if you are not planning on doing any custom catching. Try-Catch for when you are actually going to do something aside from printing the stack trace in the catch statement. – SomeStudent Aug 04 '16 at 15:50

3 Answers3

4

Is it bad to have this kind of multiple exceptios throwing from a method

No. If you use checked exceptions, this is perfectly normal. It indicates that two different exceptional things could go wrong, and allows calls to easily handle those two different exceptional conditions in two different ways.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • nice resoning. Then what if I asked you, which way is better? – Supun Wijerathne Aug 04 '16 at 15:40
  • 1
    @SupunWijerathne: I would say that calls for an opinion, and respectfully decline to comment. :-) There is a camp that feels checked exceptions are bad, including (for instance) the designers of C#. There is another camp that feels they're good, including (for instance) the designers of Java. You get to pick for yourself. ;-) – T.J. Crowder Aug 04 '16 at 15:42
  • actually I asked what is the better code style between 2 codes I posted. :) and could you please also mention what is the alternative using by C# designers? :) – Supun Wijerathne Aug 04 '16 at 15:47
  • 1
    @SupunWijerathneu: :-) It would depend on the exceptions. If they're different things you'd handle differently, I'd go for the first one. If they're very similar things and the calling code is unlikely to need to handle them separately, there might be an argument for combining them. (The C# designers went for purely unchecked exceptions -- the kind you don't have to declare, you rely on reading the documentation to know what kind of exceptions methods call, rather than having that checked by the compiler.) – T.J. Crowder Aug 04 '16 at 15:58
2

It's a perfectly acceptable practice to throw two different kinds of exceptions. This is a straight-forward way of the method declaring there are several things that can go wrong here, and give whoever's using it the opportunity to handle them separately if he wants to.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

I see nothing wrong with throwing multiple types of Exceptions in one method, the alternative would complicate things. If you throw different exceptions then the caller can handle them in a different way in a readable piece of code without having to unwrap/find root cause/etc. Note it can still be good to wrap it in another exception for various reasons like adding information but that's a different topic.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73