I am writing some library functions that needs to do some error reporting. I want to use exceptions rather than return values.
I wrote my functions that throws int exceptions.
Eg:
if(strm->atEnd()){
// unexpected end of stream
throw -2;
}
My question is, is this method OK? Or should I throw a exception derived from std::exception?
In what way is throwing a std::exception better? (Other than being able to use catch(std::exception &e))
Is throwing int exceptions bad practice? (all throw int values are documented in doxygen comments)
I can’t find any reason as to why (in this case) should I throw an object.
I would throw an exception based on
std::exception.I find this easier to
catch, log, et cetera. It also allows the removal of a comment and a magic number from the code.This message can then be sent to the end-user to give them a hope of fixing the problem.
Users and library consumers can’t read the comments in code, and they are unlikely to know what ‘-2’ means.