How can I improve my error checking and handling?
Posted
by
Google
on Programmers
See other posts from Programmers
or by Google
Published on 2011-10-27T15:50:45Z
Indexed on
2011/11/19
2:08 UTC
Read the original article
Hit count: 377
Lately I have been struggling to understand what the right amount of checking is and what the proper methods are.
I have a few questions regarding this:
What is the proper way to check for errors (bad input, bad states, etc)? Is it better to explicitly check for errors, or use functions like asserts which can be optimized out of your final code? I feel like explicitly checking clutters a program with a lot of extra code which shouldn't be executed in most situations anyway-- and not to mention most errors end up with an abort/exit failure. Why clutter a function with explicit checks just to abort? I have looked for asserts versus explicit checking of errors and found little to truly explain when to do either.
Most say 'use asserts to check for logic errors and use explicit checks to check for other failures.' This doesn't seem to get us very far though. Would we say this is feasible:
Malloc returning null, check explictly
API user inserting odd input for functions, use asserts
Would this make me any better at error checking? What else can I do? I really want to improve and write better, 'professional' code.
© Programmers or respective owner