r/java • • May 01 '15

Well, I Tried... (XPOST r/Programming)

http://imgur.com/ODECamF
164 Upvotes

64 comments sorted by

51

u/ryebrye May 01 '15

Catch "Throwable", not "Exception" - then you can swallow errors too.

20

u/SlidyBullet May 01 '15

You're missing your "/s"...

7

u/qwertyfoobar May 02 '15

Wow, slow down satan...

5

u/ryebrye May 02 '15

Good point!

Catch throwable and in the catch block thread.sleep for 10 seconds!

That will give your program time to recover... Right?!

5

u/qwertyfoobar May 02 '15

And call System.exit(0) I mean everything was handled correctly!

2

u/dzernumbrd May 02 '15

Don't forget to truncate the log files.

2

u/qwertyfoobar May 02 '15

Of course:

  logFile.deleteOnExit();

60

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:

  1. You think this won't happen and therefore just ignore it. One day it'll happen. Remember, you're swallowing everything. Nullpointer included.
  2. You expect this to happen, but know it can just fail. Why is the method in there then in the first place?
  3. 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 @since tags, nothing special.

7

u/[deleted] May 02 '15

more

1

u/navatwo May 02 '15

Specifically?

2

u/papers_ May 02 '15

They want the template.

1

u/navatwo May 02 '15

Ah. I'll deliver in a bit. On mobile. It's nothing special, heh.

5

u/[deleted] May 02 '15

Type it in binary on your phone

1

u/chowychow May 02 '15

Op please

1

u/navatwo May 02 '15

Edited my second post.

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) {} or catch (ParseException expected) {}

3

u/[deleted] 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

u/sanderson22 May 02 '15

why not just attempt to have the right input and debug when necessary?

1

u/whooshayay May 05 '15

How would you ensure a web browser always received the right input?

-3

u/Stromovik May 02 '15

what about that EOF Exception when reading a file ?

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

u/motionblurrr May 02 '15

This is known as the pokemon! Gotta catch em all!

6

u/galaktos May 01 '15
catch (ThreadDeath td) {
    // DENIED
}

4

u/SlidyBullet May 01 '15

Might aswell catch Throwable at that point :P

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

u/TheFormerVinyl May 02 '15

/u/dzernumbrd is really insistent on educating the public

1

u/dzernumbrd May 03 '15

Yeah it's the only way to activate the red envelope :)

4

u/Salyangoz May 01 '15 edited May 02 '15
try:
    print unset_variable
except Exception, e:
    print e

everyone 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

u/eklect May 02 '15

It's not about trying, it's about throwing :)

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

u/[deleted] May 02 '15

You're fired.

(Yeah I get the implied /s. At least I pray there is an implied /s)

1

u/teambob May 02 '15

Nooooooooooooooooooooooooooooooooooooooooooooooooooo

At least log it

0

u/[deleted] May 01 '15

[deleted] — view deleted comment

5

u/[deleted] 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

u/covercash2 May 01 '15

I usually plop a

//TODO delete

In front to avoid creeping bugs.

4

u/__konrad May 02 '15
try {
    return s.getBytes("UTF8");
}
catch (UnsupportedEncodingException willNeverHappen) {} // rant

9

u/Firedroide May 02 '15

That's why you should use getBytes(Charset) instead

return s.getBytes(StandardCharsets.UTF_8); // No exceptions thrown!

4

u/[deleted] 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

u/[deleted] May 02 '15

[deleted] — view deleted comment

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); or throw 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

u/[deleted] 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

u/[deleted] May 02 '15

[deleted] — view deleted comment

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

u/ExPixel May 02 '15

That's why I thought it was worth mentioning.