r/java • u/Megamax666 • May 01 '15
Well, I Tried... (XPOST r/Programming)
http://imgur.com/ODECamF60
u/SlidyBullet May 01 '15
Bad.
Bad bad bad bad bad bad bad bad bad bad bad bad bad bad.
At least print the exception out. There's three situations:
You think this won't happen and therefore just ignore it. One day it'll happen. Remember, you're swallowing everything. Nullpointer included.
You expect this to happen, but know it can just fail. Why is the method in there then in the first place?
You don't care that the code throws an exception. If you ever expand your project, you will care.
In all 3 situations, you should at the very least just use e.printStackTrace() (or better yet, log it somewhere)...
9
u/navatwo May 01 '15
I have eclipse set up to automatically rethrow in an invalid state exception by default. It has found so many bugs.
7
u/sudonathan May 01 '15
Is this in your default exception generation template? Can you explain in depth more please?
11
u/navatwo May 01 '15 edited May 02 '15
Yeah! It is. I use this because eating exceptions is generally a bad thing. Most bad code happens because people just try to remove the checked exception on the method signature. Exceptions are there because they are to be handled and flow needs to change. If you ignore them, you're defeating the purpose of what the developer chose to have you handle. I'm on mobile so my description is rather limiting. If you want more, tell me.
E: I put all of my templates in a gist. Most I just added
@sincetags, nothing special.7
May 02 '15
more
1
u/navatwo May 02 '15
Specifically?
2
u/papers_ May 02 '15
They want the template.
1
1
u/caltheon May 02 '15
The one place I do this is when parsing dates that I know can be bad and don't care if they are. There is literally no reason to handle the exception. I do put comments in though.
4
u/shadowdude777 May 02 '15
In those cases I like to make it very obvious that I did it on purpose by renaming the variable.
catch (ParseException ignored) {}orcatch (ParseException expected) {}3
May 02 '15
I prefer to mark intentionally empty scopes with a comment:
catch (ParseException ignored) { /* Do nothing */ }3
u/dzernumbrd May 02 '15
You catch a ParseException though not an Exception. Catching Exception is very broad, ParseException is surgical.
1
u/caltheon May 02 '15
Yes, I thought the hate was on empty catch blocks. Though the only other exception would be if the parser was null.
1
u/qroshan May 02 '15
No you log it. Always log it
1
u/caltheon May 03 '15
No, this is an optional field that sometimes contains dates. If it has a date, I use it, if it doesn't, I don't. Sometimes it's null, sometimes its other useless data. There is zero reason to log it since my program doesn't care if it's not a date and it's fine for it not to be a date (or be null)
1
u/qroshan May 03 '15
You obviously want to know what %age of data has legitimate dates and what doesn't. You can also correlate other things like when record X doesn't have a date, it does / doesn't do Y.
Sometime in the future when you have forgotten the code, this nodate will trip you somewhere else and you'll have no idea what happened.
1
u/caltheon May 03 '15
Trust me. That data is meaningless. If I wanted it I would just write a sql query. Ovsrthinking like that is far more harmful
0
u/qroshan May 03 '15
writing logs is not overthinking.
Logs > SQL queries. SQL (RDBMSs) store data as logs.
0
u/whooshayay May 05 '15 edited May 05 '15
If the data is meaningless, why are you parsing it in the first place?
My take on this is that if you work in web or security, you want to log that. You may have to deal with malicious input that exploits some very subtle flaw in your application or the date library it is using. Without a log you won't know where that is coming from. Of course, I haven't see your code and don't know what you're doing, so this may not apply to you.
-2
-3
18
u/byeSpideR May 02 '15
I think people are missing the meaning behind this meme...
19
u/ezql May 02 '15
bad advice mallard..... wrong crowd maybe?
10
u/DanLynch May 02 '15
Wrong crowd indeed. Not everyone on reddit subscribes to /r/AdviceAnimals or is familiar with the image macro subculture.
8
6
9
u/omgdonerkebab May 02 '15 edited May 02 '15
There's a section in every Java book about not doing this.
Perhaps we should be alarmed that this is true of both intro books and books geared towards seasoned software developers.
2
4
u/Salyangoz May 01 '15 edited May 02 '15
try:
print unset_variable
except Exception, e:
print eeveryone lost their minds at this. I've no idea why to this day. I dont do it but I dont know why I dont do it. I know how to set up variables the part about "Exception,e" is what I dont get atm.
edit: I was drunk yesterday and thought I was in /r/python .
1
1
u/chunkyks May 02 '15
I have this at the top of every swing app I create:
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception ex) {
// Can't claim I didn't try
}Because there's nothing I really care about; if the look and feel setter succeeds, awesome. If not... there's still going to be something there. And if not, I have bigger concerns.
1
u/slartybartfast_ May 05 '15
Yoda had it right: Do or not do. There is no try.
In other words: If you don't care about the exception in your business case, just throw it.
1
1
0
May 01 '15
[deleted]
5
May 01 '15
I never thought of this :O I always add a "// Exception ignored because...". I may steal this idea and add it in
9
4
u/__konrad May 02 '15
try { return s.getBytes("UTF8"); } catch (UnsupportedEncodingException willNeverHappen) {} // rant9
u/Firedroide May 02 '15
That's why you should use getBytes(Charset) instead
return s.getBytes(StandardCharsets.UTF_8); // No exceptions thrown!4
May 02 '15
In such situations, do something like this:
try { return s.getBytes("UTF8"); } catch( UnsupportedEncodingException encodingEx) { throw new RuntimeException( "Program logic exception: hardcoded charset name misspelled?", encodingEx ); }
0
May 02 '15
[deleted]
2
u/SoniEx2 May 02 '15 edited May 06 '15
Empty catch blocks are the devil's work.
try { ByteArrayInputStream bais = new ByteArrayInputStream(...); // stuff that cannot cause an IOException bais.close(); } catch (EOFException e){ // do your normal EOF handling here } catch (IOException ignored){ /* optionally halt and catch fire */ }Sorry, I don't see why it's bad... when used correctly.
EDIT: meh w/e
10
u/SlidyBullet May 02 '15
Accidentally read more than is available? Fuck up a possibly set mark?
If you are absolutely sure it won't happen,
throw new AssertionError(String, Throwable);orthrow new RuntimeException(String, Throwable);should be used.3
u/SoniEx2 May 02 '15
Accidentally read more than is available? Fuck up a possibly set mark?
Doesn't throw IOException. (as I said, ByteArrayInputStream)
6
u/yawkat May 02 '15
Still better to throw an assertionerror in that case just in case your code is being weird. Also helps with variables that leave the try block.
4
May 02 '15
Still better to throw an assertionerror in that case just in case your code is being weird.
Or one of your coworkers change it from a ByteArrayInputStream to ReallyHotNewFrameworkInputStream at some point.
0
May 02 '15
[deleted]
5
u/ExPixel May 02 '15
This meme is called bad advice mallard :|
4
u/DanLynch May 02 '15
Not everyone on reddit subscribes to /r/AdviceAnimals or is familiar with the image macro subculture. There is a reason why many subreddits prohibit image macro posts like this.
1
51
u/ryebrye May 01 '15
Catch "Throwable", not "Exception" - then you can swallow errors too.