[英]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?... 这个throw()和通常使用的是什么区别?...
} 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. Java就是其中之一。 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);
但是,不要选择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;
喜欢正常throw e;
syntax for clarity. 语法清晰。
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 是equulivilant
Public int get value() {
return (3);
}
It's the same with throwable. 它与throwable相同。
In face they will complied to the exactly same thing. 面对面,他们将遵循完全相同的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.