4

I came across this rethrown exception and am surprised that it even compiles.

} catch(SomeException e) {
    ...
    throw(e);
}

Is there any difference between this throw() and what is normally used?...

} catch(SomeException e) {
    ...
    throw e;
}

Any links to where this is documented or guidance on choosing one over the other?

noctonura
  • 12,763
  • 10
  • 52
  • 85
  • 4
    They are the same – Mustapha Belmokhtar Dec 15 '17 at 21:48
  • 1
    Looks like it's just superfluous grouping parentheses. – Carcigenicate Dec 15 '17 at 21:48
  • Note that you shouldn't log and throw. If you catch it somewhere else and log it there too, you'll get the mistaken impression there were two exceptions. – Andy Turner Dec 15 '17 at 21:49
  • There is a similar c++ question and the answer says no difference, https://stackoverflow.com/q/14321163 – Emre Dec 15 '17 at 21:50
  • @AndyTurner depends. It's common that you want to log a problem somewhere where there is more context to the problem, but throw since you don't want to stop exception from propagating. – eis Dec 15 '17 at 21:51
  • 2
    @eis In that case the context should be recorded in the exception itself. And while we're at it, logging `e.getMessage()` is a bad idea whether you rethrow or not. – biziclop Dec 15 '17 at 21:51
  • 4
    I removed the log() line so as not to cloud the issue of the question. – noctonura Dec 15 '17 at 21:53
  • ..and when logging, you usually want the stack trace of the exception to be logged as well (`myLogger.error(e.getMessage(), e)`). `e.getMessage` often doesn't give enough information. – Mick Mnemonic Dec 15 '17 at 21:53
  • @biziclop again, depends. It might be information that you do *not* want to expose to end user (where the exception might end up), but it is useful diagnostic in the logs. However I do agree that logging e.getMessage() is almost always wrong thing to do. – eis Dec 15 '17 at 21:55
  • Basically `throw` accepts any expression that evaluates to an instance of `Throwable`. So you could even do `throw constructMyException();` if you've got a method `Exception constructMyException() {...}`. You can even do `throw null;`, but that's particularly naughty. And of course `(e)` is a valid expression and evaluates to ... well, `e`. – biziclop Dec 15 '17 at 21:55
  • `(they are)` same as `they are` – jmj Dec 15 '17 at 22:02
  • 1
    @eis you're talking about logging information that's not in the exception. I'm not objecting to that, I'm talking about not logging the exception and then throwing *the same* exception. – Andy Turner Dec 15 '17 at 22:09

3 Answers3

9

Quite a few languages allow as many parenthesis around expressions as you want. Java is one of them. The following is perfectly valid code.

public class HelloWorld {
  public static void main(String[] args) {
    throw ((((new RuntimeException()))));
  }
}

So there's absolutely no difference, except that your source file is two bytes larger.

kelunik
  • 6,750
  • 2
  • 41
  • 70
  • 2
    "So there's absolutely no difference, except that your source file is two bytes larger." And much less readable. – davidxxx Dec 15 '17 at 21:56
6

Functionally they are equivalent.

However, don't choose throw(e);, as someone might mistake it for a method call, and the very least will make someone unnecessarily wonder what it is that you're doing. Prefer the normal throw e; syntax for clarity.

eis
  • 51,991
  • 13
  • 150
  • 199
  • 2
    Right. Unnecessary parenthesis that are neither functionally required nor a readability improvement are error prone and dead code. – davidxxx Dec 15 '17 at 22:07
0

Throw is an instruction to throw a "throwable" (usually an exception)

Think if it like a return statement

Public int get value() {
    return 3;
}

Is equlivilant of

Public int get value() {
    return (3);
}

It's the same with throwable.

In face they will complied to the exactly same thing.

MartinByers
  • 1,240
  • 1
  • 9
  • 15