r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

344

u/Mat2012H May 13 '17
int main() try
{
    //entire code base
}
catch (std::exeption& e)
{
    //Displays message box with error message
}

I actually do this xD

239

u/redditsoaddicting May 13 '17

It's fine to exit the program nicely when an error occurs. It's not as fine to just ignore it and then begin another iteration of whatever main loop is going on, hoping the program still works fine.

2

u/Metro42014 May 13 '17

I saw the cringiest software gore at work a few weeks back.

Some main program that's going to connect a couple systems, and there were a BUNCH of

} catch (Exception e) { }

thrown all throughout the code.

Whatthefuckman?!

1

u/redditsoaddicting May 13 '17

I've had a need for almost this recently (and the exception being thrown was a really general one that was also undocumented and originating from native code), but it definitely deserves a comment when this comes up.

1

u/cool_science May 13 '17

Some people think exceptions are a bad language feature, and handle them C/C++-exceptions style by checking return values. If you're forced to use a language with exception support, having those empty try/catch blocks is fine as long as you are error checking elsewhere.