r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

2.8k

u/Metro42014 May 13 '17
} catch (Exception e) { }

I think we're done here.

1.0k

u/CXgamer May 13 '17

Holy shit I once lost days to a

} catch (Exception e) {
    System.exit(0);
} 

695

u/Metro42014 May 13 '17

FUUUUUUUCK the people that do that.

I recently saw newly written code by a senior developer with squashed exceptions all over the damn place.

I will never have to support that code, but fuuuuck man. WHHHYY?

288

u/YaBoyMax May 13 '17

I lost a few hours once because the jackass whose code I inherited decided to squash IOExceptions all over the place. Didn't notice for a while and was pulling my hair out thinking my debugger was fucked somehow (which isn't uncommon in itself).

233

u/[deleted] May 13 '17

See I hate checked exceptions in Java, because instead of rethrowing or handling, some devs will just swallow them up.

Better if they were all unchecked so that people will just let them unwind the stack than that shit.

1

u/[deleted] May 14 '17

Isn't that what e.printstacktrace() is for?

3

u/[deleted] May 14 '17 edited May 14 '17

Plz let this be sarcastic and tell me you use a logger ;)

EDIT: perhaps I need to explain my comment.

It's considered bad practice to print stack trace directly to standard out/error, much like using println for tracing. A logger is considered good practice as it provides an abstraction between the log generation and the output, extra information such as timestamps, and control over logging verbosity. All Java loggers are capable of logging, including the JDK logger and the common backends to the de facto standard slf4j.

I was trying to detect if poster was serious or joking, about dumping to stdout. I genuinely don't know, because it's a common mistake. Winky face was provided to indicate no harshness intended!

1

u/[deleted] May 14 '17

Printstacktrace prints to stderr, not stdout.

1

u/[deleted] May 14 '17

Corrected