简体   繁体   English

为什么我要提供标识符?

[英]Why do I have to give an identifier?

In code: 在代码中:

try
{
    System.out.print(fromClient.readLine());
}
catch(IOException )//LINE 1
{
     System.err.println("Error while trying to read from Client");
}

In code line marked as LINE 1 compiler forces me to give an identifier even though I'm not using it. 在标记为LINE 1的代码行中,编译器强制我提供标识符,即使我没有使用它。 Why this unnatural constrain? 为什么这种不自然的约束? And then if I type an identifier I'm getting warning that identifier isn't used. 然后,如果我键入一个标识符,我会收到警告,标识符未被使用。 It just doesn't make sense to me, forcing a programmer to do something unnecesarry and surplus. 这对我来说没有意义,迫使程序员做一些不必要的和剩余的事情。 And after me someone will revise this code and will be wondering if I didn't use this variable on purpouse or I just forgot. 在我之后,有人会修改这段代码,并且想知道我是不是在purpouse上使用这个变量,或者我忘记了。 So in order to avoid that I have to write additional comment explaining why I do not use variable which is unnecessary in my code. 所以为了避免这种情况,我必须编写额外的注释来解释为什么我不使用我的代码中不必要的变量。
Thanks 谢谢

The identifier name is required to make parsing simpler for the compiler. 标识符名称是使编译器的解析更简单所必需的。 And omitting the exception in a catch clause is considered bad practice IMHO rarely a good idea - in production code you should (almost always) print / log it and/or rethrow it. 并且在catch子句中省略异常被 认为是不好的做法 恕我直言很少有好主意 - 在生产代码中你应该(几乎总是)打印/记录它和/或重新抛出它。 So not using the identifier name should be the exception (no pun intended) rather than the rule. 因此,不使用标识符名称应该是例外(没有双关语)而不是规则。

But if you really have a good reason to omit not use it, you can add a comment to explain your point, [Update2] and/or to tell your IDE to suppress that kind of inspection for that specific code block (most IDEs allow this) [/Update2] . 但是如果你真的有理由 省略 不使用它,你可以添加一个注释来解释你的观点, [Update2]和/或告诉你的IDE禁止对那个特定的代码块进行那种检查(大多数IDE都允许这样做) ) [/ Update2]

Update: OK, "bad practice" may be too strong a word :-) Let me try to explain my point better. 更新:好的,“糟糕的做法”可能太强了一句话:-)让我试着更好地解释我的观点。

What I mean is that typically when you catch an exception, it is preferred to try to log / use as much information out of it as you can. 我的意思是,通常当您捕获异常时,最好尝试记录/使用尽可能多的信息。 Which implies that you actually refer to the exception variable. 这意味着您实际上引用了异常变量。

In your example above, if you only log "Error while trying to read from Client", that is a very limited amount of information. 在上面的示例中,如果您只记录“尝试从客户端读取时出错”,那么信息量非常有限。 The actual cause of the problem might be (just a few guesses) a corrupted file, a network error, a formatting error ... Logging the data within the IOException would provide much more detail about it, making the problem easier to fix. 问题的实际原因可能是(只是几个猜测)一个损坏的文件,网络错误,格式错误......在IOException中记录数据将提供更多关于它的细节,使问题更容易修复。

The compiler is correct in calling you on this. 编译器在调用此方时是正确的。 According to the Java grammar , the "parameter" to a catch clause must have both a type and a variable identifier. 根据Java语法 ,catch子句的“参数”必须同时具有类型和变量标识符。 If it is omitted, your program text is no longer a syntactically correct Java program. 如果省略,则程序文本不再是语法正确的Java程序。

CatchClause: catch ( FormalParameter ) Block CatchClause:catch(FormalParameter)Block

FormalParameter: [final] Type VariableDeclaratorId FormalParameter:[final] Type VariableDeclaratorId

As for why things are this way - I think it's easier to parse, and allowing an option here doesn't provide you with anything. 至于为什么事情是这样的 - 我认为它更容易解析,并且在这里允许选项并不能为您提供任何东西。 It is very common to react to an error by making use of the exception object, so the typical case is to have a variable, not to omit it. 通过使用异常对象来对错误做出反应是很常见的,因此典型的情况是拥有一个变量,而不是省略它。 Since there is no added performance cost, they probably just went with allowing you to not use it. 由于没有增加的性能成本,它们可能只是允许您不使用它。 In my experience no IDE warns you about an unused variable if you don't refer to the exception object within your catch block. 根据我的经验,如果您没有引用catch块中的异常对象,则没有IDE会警告您有关未使用的变量。

Simplifying the other answers - I think - when you catch an exception, you need to actually catch something, and you're specifying what that's going to be. 简化其他答案 - 我认为 - 当你遇到异常时,你需要实际捕获一些东西,然后你要指明那将是什么。 It's not "execute this code if something goes wrong", it's "execute this code if this particular thing goes wrong". 它不是“如果出现问题就执行此代码”,它是“如果这个特殊的东西出错就执行这个代码”。

Best practice is to log or rethrow; 最佳做法是记录或重新抛出; if you're catching an exception that's not something you want to log or rethrow, did you really need to catch it, or could you have done it another way? 如果你正在捕捉一个想要记录或重新抛出的异常,你真的需要抓住它,还是你可以用另一种方式完成它?

In your catch, you are defining the variable that is caught, not just the exception's class. 在catch中,您定义的是捕获的变量 ,而不仅仅是异常的类。 It's similar to IOException ex = new IOException() semantically. 它类似于IOException ex = new IOException()语义上。 And you should actually be using the variable. 你应该实际使用变量。 See all the rational in Is it okay that I sometimes sink my exceptions here on SO. 看看所有的理性我是否可以将我的例外情况放在SO上。 Logging the exception, as you are doing, provides some information. 正如您所做的那样记录异常提供了一些信息。 Ideally, though, your code should handle the exception in some way, even if it is just to report the error to the client (which, if your app is a console app, it would to be doing). 理想情况下,您的代码应该以某种方式处理异常,即使它只是向客户端报告错误(如果您的应用程序是控制台应用程序,它将会这样做)。

If you have catch(IOException ex) , you can also get the full stack trace with ex.printStackTrace() , use localized messages, and more. 如果你有catch(IOException ex) ,你也可以使用ex.printStackTrace()获取完整的堆栈跟踪,使用本地化的消息等等。 It's good for debugging. 这对调试很有帮助。 Try it! 试试吧!

You can get more information in the Exceptions tutorial . 您可以在Exceptions教程中获取更多信息。

As an interesting note, in Java 7 the syntax for catching exceptions is expected to change slightly, as multicatch and final rethrow were excepted. 有趣的是,在Java 7中,捕获异常的语法预计会略有变化,因为多重捕获和最终重新抛出都是例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM