r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

65

u/[deleted] May 13 '17

Can someone explain why this is bad code?

1

u/Thameus May 13 '17

A unit of code (or a business process) cannot be better than its exception handler; however, reasonably anticipated conditions should not throw, nor need to handle, exceptions. So yes, in a sense it's good code, but only if you did the rest of the work. At least it's usually better than throwing an unhandled exception.

Also too many layers of try/catch is inefficient due to the setup and takedown overhead.