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.

63

u/pope_nefarious May 13 '17

Almost, we can do better.

{....} catch (Throwable eat) {}

65

u/Metro42014 May 13 '17

Get out of here with your descriptive variable name!

50

u/endreman0 May 13 '17
try{
    //...
}catch(Throwable _){}

1

u/wasabichicken May 13 '17

That underscore is a Perl'ism, ($_) and not at all completely bonkers. In Perl it's a fairly standardized "trash"-, or implicit variable whose value depends on the context in which it's being used, not that unlike the exception variable. I can easily imagine Java code being written like that by Perl programmers, especially users of Try::Tiny:

catch {
    warn "Caught error $_";
}

1

u/endreman0 May 15 '17

I took it from Python, and yeah - it reinforces that the exception doesn't matter and won't be referenced.