Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "throw(e)" and "throw e"?

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?

like image 302
noctonura Avatar asked Dec 06 '22 12:12

noctonura


2 Answers

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.

like image 171
kelunik Avatar answered Dec 26 '22 14:12

kelunik


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.

like image 43
eis Avatar answered Dec 26 '22 14:12

eis