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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With