Why should I 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 a line of code marked as LINE 1 compiler, it forces me to specify an identifier, even if I do not use it. Why is this an unnatural restriction? And then, if I print the identifier, I get a warning that the identifier is not used. It just doesn't make sense to me, forcing the programmer to do something unnecessary and redundant. And after me, someone will review this code and will be wondering if I used this variable on purpouse or just forgot. Therefore, to avoid this, I must write an additional comment explaining why I am not using a variable that is not needed in my code.
Thanks

+4
source share
4 answers

An identifier name is required to simplify parsing for the compiler. And throwing an exception in a catch clause means that it is considered bad practice. IMHO is rarely a good idea - in production code you should (almost always) print / write it and / or reconstruct it. Therefore, do not use the identifier name should be an exception (not intended for puns), and not a rule.

But if you really have weight so omit does not use it, you can add a comment to explain your point, [Refresh2] and / or tell the IDE to suppress this kind of check for this particular code block (most IDEs allow this) [/ Update2] .

Update: OK, “bad practice” might be too strong a word :-) Let me try to explain my point better.

What I mean is that usually when you encounter an exception, it is preferable to try to use / use as much information from it as possible. This means that you are really referring to an exception variable.

In the above example, if you only register the message "Error while trying to read from the client", this is a very limited amount of information. The actual cause of the problem may be (just a few guesses) a damaged file, a network error, a formatting error ... Registering data in IOException will provide much more detailed information about this, making it difficult to fix the problem.

+10
source

The compiler correctly called you. According to the Java grammar, the parameter parameter in catch must have both type and variable identifier. If it is omitted, your program text is no longer the syntactically correct Java program.

CatchClause: catch (FormalParameter) Block

Formal parameter: [final] Type VariableDeclaratorId

As for why this happens, I think it's easier to make out, and the choice here gives you nothing. Very often you have to respond to an error using an exception object, so a typical case is to have a variable, not to omit it. Since there is no extra cost to performance, they probably just went, letting you not use it. In my experience, the IDE does not warn you about an unused variable unless you reference an exception object in your catch block.

+5
source

Simplification of other answers - I think - when you catch an exception, you need to catch something, and you specify what it will be. It is not "execute this code if something goes wrong," it will "execute this code if this particular thing goes wrong."

Best practice is to log or retronize; if you catch an exception that you don’t want to register or re-register, do you really need to catch it, or could you do it differently?

+1
source

In your catch, you define a caught variable, not just an exception class. This is similar to IOException ex = new IOException() semantically. And you really have to use a variable. See All rational in Is it good that I sometimes lose my exceptions here on SO. When registering an exception, as you do, some information is provided. Ideally, although your code should somehow handle the exception, even if it just reports an error to the client (which, if your application is a console application, it should have done).

If you have a catch(IOException ex) , you can also get a full stack trace using ex.printStackTrace() , use localized messages, etc. This is good for debugging. Give it a try!

You can get more information in the Exception Tutorial .

Interestingly, in Java 7, the syntax for throwing exceptions is not expected to change much since multicatch and final retron are excluded.

0
source

Source: https://habr.com/ru/post/1309005/


All Articles