为什么java中的catch块中需要括号?
在java中,如果我们只需在if
或for
之后执行一条语句,则不需要括号。我们可以写:
if(condition)
executeSingleStatement();
or
for(init;condition;incr)
executeSingleStatement();
但是在catch块的情况下为什么我们不能省略括号呢?为什么这是不可能的?
catch(Exception e)
e.printStackTrace();
因为在大多数情况下,we 我在 catch 块中只有一个语句,它是 e.printStackTrace()
while test 或logging 语句。
In java if we have to execute only one statement after if
or for
the brackets are not necessary. We can write:
if(condition)
executeSingleStatement();
or
for(init;condition;incr)
executeSingleStatement();
But in the case of catch block why we can not omit the brackets? Why this is not possible?
catch(Exception e)
e.printStackTrace();
Because in most of the case we I have only one statement in catch block which is either e.printStackTrace()
while testing or logging statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
找到一本编译器构建教科书并查找dangling-else 歧义。
鉴于在 Java 和大多数其他语法糟糕的语言中,空格是谎言。您如何解释:
是:
还是:
通过将
else
与最内层的if
关联来解决悬挂-else 歧义。我们是否要添加更复杂的复杂功能来处理这种糟糕的风格?否。编辑:有一条评论指出该示例未涵盖
catch
。在try
上需要大括号,但在catch
/finally 上不需要大括号,这将是一个正确奇怪的决定代码>.但无论如何,为了完整性,请考虑以下代码。
BazException
和finally
的catch
与内部还是外部try
关联?语言设计委员会再次可以添加大量语法来消除歧义,但明确的风格再次获胜。如果语言被简化为在各处强制使用显式大括号,我在 Sun/Oracle 的工作会容易一些。Find a compiler construction textbook and look-up the dangling-else ambiguity.
Given that in Java, and most other languages with horrible syntax, spacing lies. How do you interpret:
Is it:
Or:
The dangling-else ambiguity is resolved by associating the
else
with the inner-mostif
. Do we want to add a more complicated complication to handle this poor style? No.Edit: There's a comment that the example does not cover
catch
. It would be a proper weird decision to require braces ontry
but notcatch
/finally
. But anyway, for completeness, consider the following code.Is the
catch
ofBazException
andfinally
associated with the inner or outertry
? Again the language design committee could have added a ton of grammar to disambiguate, but again explicit style wins. My job at Sun/Oracle would have been a little easier if the language had been simplified to mandate explicit braces everywhere.这不是一个可能或不可能的问题。这只是一个语言(语法)设计决策。
Java语言解析器有多种实现。人们可以在不到一天的时间内修改解析器源代码并允许不使用括号括起来的 catch 语句。
http://www.google.com/search?q=java+parser
另请注意 Java 语言语法。
It's not an issue of possible or impossible. It's just a language (syntax) design decision.
There are several implementation of Java language parser. One could modify the parser source less than a day and allow no-bracket-enclosed catch statements.
http://www.google.com/search?q=java+parser
Also note Java language grammar.
我不知道为什么 Java 不允许这样做,但一般来说,即使只有一个语句,使用括号也是更好的风格。它使它更容易阅读和扩展。
这是一个相关的问题,解决是否使用括号:JavaScript 中的单行语句是否需要大括号?
I'm not sure why Java doesn't allow that but generally speaking it is better style to use brackets even if there is only one statement. It makes it easier to read and expand.
Here is a related question that addresses whether to use brackets or not: Are curly braces necessary in one-line statements in JavaScript?
来自 Java Language Spec 3.0 - 如果您查看第14章,讨论了块和语句。块由 { 和 } 标识并包含许多语句。 Try/catch/finally 是块,根据语言规范需要将其分组在 { } 中。
From the Java Language Spec 3.0 - If you look at Chapter 14, it talks about Blocks and Statements. Blocks are identified by { and } and contain many statements. Try/catch/finally are blocks, which per the language spec need to be grouped in { }.
我也没有看到编写无用代码有任何好处,而且我也不明白更多的文本如何变得更容易。
我通过在一行中编写一行捕获来帮助自己:
对我来说,这是最易读的方式。当人们试图将完整的小说写成例外名称时,它只会变得混乱。
I also do not see any benefit in writing useless code and I also do not understand how more text could be easier.
I help myself by writing one-line-catches in exactly one line:
For me this is the most readable way. It gets only cluttered when people try to write complete novels into exception names.
我认为“在大多数情况下”您应该做的不仅仅是打印或记录堆栈跟踪。这意味着您正在接受异常,这通常不是一个好的做法。至少,如果您无法从异常中正常恢复,您应该记录并重新抛出它,以防堆栈上方的代码可以。
也就是说,我认为除了“这就是他们的设计方式”之外,你的问题没有真正的答案。
I would argue that "in most cases" you should be doing more than just printing or logging the stack trace. This means you are swallowing an exception, which is not generally a good practice. At the least, if you cannot gracefully recover from the exception, you should log and rethrow it in case code higher up the stack can.
That said, I don't think there is really an answer to your question beyond "that's just how they designed it".
可能是继承自C++。不知道为什么 C++ 这样做。在这里查看我的想法: https://stackoverflow.com/questions/6254595 /how-do-you-use-the-return/6255489#6255489
还值得注意的是
{}
使语法更加简单,即使语言设计者的工作更加轻松。if
语句就是一个很好的例子 - 您想要的便利并不便宜。由于像
if
的语法这样的歧义,必须进行调整才能解决歧义。假设需要
block
而不是任意statement
,由于{}
的存在,语法会简单得多。所以他们可能只是感到懒惰。
Probably inherited from C++. No idea why C++ did that. See my thoughts here: https://stackoverflow.com/questions/6254595/how-do-you-use-the-return/6255489#6255489
It's also worth noting that
{}
makes grammar simpler, i.e. makes language designer's life easier. Theif
statement is a good example - the convenience you like does not come cheaply.Because of the ambiguity like
The grammar of
if
has to be tweaked to resolve the ambiguity. Supposeblock
is required instead of arbitrarystatement
, the grammar would be much simpler because of the presence of{}
.So they were probabaly just feeling lazy.