I'm trying to convince my team lead to allow using exceptions in C++ instead of returning a bool isSuccessful
or an enum with the error code. However, I can't counter this criticism of his.
Consider this library:
class OpenFileException() : public std::runtime_error {
}
void B();
void C();
/** Does blah and blah. */
void B() {
// The developer of B() either forgot to handle C()'s exception or
// chooses not to handle it and let it go up the stack.
C();
};
/** Does blah blah.
*
* @raise OpenFileException When we failed to open the file. */
void C() {
throw new OpenFileException();
};
Consider a developer calling the
B()
function. He checks its documentation and sees that it returns no exceptions, so he doesn't try to catch anything. This code could crash the program in production.Consider a developer calling the
C()
function. He doesn't check the documentation so doesn't catch any exceptions. The call is unsafe and could crash the program in production.
But if we check for errors in this way:
void old_C(myenum &return_code);
A developer using that function will be warned by the compiler if he doesn't provide that argument, and he'd say "Aha, this returns an error code I must check for."
How can I use exceptions safely, so that there's some sort of contract?
Either
/Result
monad to return the error in a type-safe composable way