1 karma · joined May 6, 2012 (14y ago) · 1 submissions
comment 14y ago · link · parent
When a function is unable to perform its task, throw an exception. It's that simple. Do not use error codes to tell the caller "I cannot do what you asked", throw an exception.
Here are some reasons:
1) Programmers pathologically ignore or forget to check return codes from functions and now there are hard to find bugs in the program
2) A clean and beautiful algorithm is made obscure by messy error handling code
3) Indecision and disagreement about how to deal with errors in a program: throw exceptions
4) Return codes cannot provide enough information about an error in order to decide how to handle it
5) Many functions cannot return error codes (Constructors, operator overloads, conversion operators)
6) Return codes corrupt the meaning of the word "function" and destroy the code's usability as a function
7) The need to signal an error condition in code that has no provision for dealing with errors because when it was written no one expected that an error could occur here
8) Functions tangled with return code checking, propagate their problems to anyone else using them, spreading complexity like a cancer
9) Error handling code doesn't get tested
10) Consistent error handling with return codes triples your code size because every function call will need "if fail then return fail code"
11) Errors are occurring in the program but where to look in tens of thousands of lines of code? Exception constructors can log when, where and how telling you exactly where to look.