views:

195

answers:

4

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. 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. 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

+10  A: 

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. 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].

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.

Péter Török
"Considered harmful" articles considered harmful.
Michael Krelin - hacker
@Peter but I'm not ommiting this exception. I'm just not using it. Do you wanna tell me that not using an exception in catch clause is a bad practice too?
There is nothing we can do
@Knowing me knowing you - You should definitely do something with caught exceptions. See http://stackoverflow.com/questions/2142184/is-it-okay-that-i-sometimes-sink-my-exceptions
justkt
That is exactly what he's trying to tell you (I also disagree with this generalization).
Michael Krelin - hacker
+1 because making parsing simpler is probably the reason. @Knowing has a point, though. Once you've identified the type of the exception, you don't necessarily have to use the reference in order to handle it.
Bill the Lizard
@Michael - so you're telling me that I actually have to find an artificial reason to use this exception even though my code doesn't actually need it? Otherwise it will be considered as a bad practice?
There is nothing we can do
sleske
@Peter to your last paragraph "...that is a very limited amount of information...". That is the exact information I wanted to have but because of this unreasonable constrain I had to create a variable which I've never used. Decide for yourself what's better: Trust the programmer and let him do what he think he's right or impose number of constraints on a programmer and force him to create code with superflous objects in it. Decide for yourself.
There is nothing we can do
@Knowing me knowing you, I don't disagree with this. I tried to describe the "common case". And that the designers of Java decided to optimize for that, let's say, 99% of the cases. And I added "But if you really have a good reason to not use it, you can add a comment to explain your point". To which I now add "and/or suppress code inspection for that statement within your IDE".
Péter Török
@Peter what kind of optimization you talking about?
There is nothing we can do
@Knowing me knowing you, optimizing (in this case, simplifying) the language definition and thus the parsing of source code.
Péter Török
@Peter If that's the case, you should pay more attention to words you are using because optimization and simplification are nowhere near to each other when meaning is concerned. Just think if something which is optimized and something which is simplified have the same meaning.
There is nothing we can do
@Knowing me knowing you, I used the (IMHO well known) expression "optimizing for the common case" not in any strict (computer science / math / ...) sense. IMHO it would have been more complicated to try to express the same meaning using the word "simplify". Meaning is a multi-level thing.
Péter Török
@Peter No Peter, if you talking about something, and the cause of the form in which the subject is in, you should be precise. Simplifying != Optimizing. In your case you should say it was simplified for the common case.
There is nothing we can do
@Knowing me knowing you, one can never be careful enough to express one's opinion precisely :-) Thanks for your feedback, I will take it into account in the future.
Péter Török
A: 

In your catch, you are defining the variable that is caught, not just the exception's class. It's similar to IOException ex = new IOException() semantically. 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. 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. It's good for debugging. Try it!

You can get more information in the Exceptions tutorial.

As an interesting note, in Java 7 the syntax for catching exceptions is expected to change slightly, as multicatch and final rethrow were excepted.

justkt
@justkt read my comment to Peter's answer
There is nothing we can do
@Knowing me knowing you - I'm with Peter, though. You shouldn't sink exceptions. Especially not in an application with a graphical interface where something is wrong, and the user doesn't know it (unless your code continues perfectly normally with that error, which is extremely unusual). See the SO link I linked for generous, copious rationale and lots of agreement from the community.
justkt
@justkt but I'm not sinking this exception. I'm catching it and I'm informing a user what happened. If I wouldn't do anything with that that would be sinking.
There is nothing we can do
@Knowing me knowing you - OK, so this is a console app then. Got it. Even still, if you as the programmer need to come along and debug this, I'd want the actual exception object (with associated method and stack trace) to look at, you know?
justkt
@justkt so then you can simply add an identifier and be happy, you know?
There is nothing we can do
@justkt I'm sorry but I have to say that in response to your last comment: This is a typical java like way of thinking which basically mean that they (Java inventors) try to foresee (but never succeded in my opinion) what the programmer need/have/must/may to do and in the same way are placing illogical constraints in form of illy formulated language grammar. If as you say later someone want to debug this, no problem add a variable and debug. But don't force me to do it because someone one day may be wanting to use that. Ridiculous.
There is nothing we can do
@Knowing - I've never caught an exception and not put the stack trace in a log so I, as the developer, can use it for debugging purposes (at least) - with the possible exception of `ParseException`, which ought to have been unchecked in many developer's perspectives. So I guess personally I find the object extremely useful.
justkt
+5  A: 

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. If it is omitted, your program text is no longer a syntactically correct Java program.

CatchClause: catch ( FormalParameter ) Block

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.

Uri
+1  A: 

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?

Dean J
@Dean I absolutely can't agree with you. It's me who decides what to do with exception and if I wan't to do this my way I should be able to do so. I don't think creating an unused variable is a good practice either.
There is nothing we can do