I am not a programmer, but I learned a lot by watching others. I write wrapper classes to make things easier with the really technical API I'm working with. Its procedures return error codes, and I have a function that converts them to strings:
static const char* LibErrString(int errno);
For consistency, I decided that a member of my classes throws an exception when an error occurs. I created a class:
struct MyExcept : public std::exception {
const char* errstr_;
const char* what() const throw() {return errstr_;}
MyExcept(const char* errstr) : errstr_(errstr) {}
};
Then in one of my classes:
class Foo {
public:
void bar() {
int err = SomeAPIRoutine(...);
if (err != SUCCESS) throw MyExcept(LibErrString(err));
}
};
Everything works fine: if it SomeAPIRoutinereturns an error, the try-catch block around the call Foo::barcatches the standard exception with the correct error string in what().
Then I wanted the member to provide additional information:
void Foo::bar() {
char adieu[128];
int err = SomeAPIRoutine(...);
if (err != SUCCESS) {
std::strcpy(adieu,"In Foo::bar... ");
std::strcat(adieu,LibErrString(err));
throw MyExcept((const char*)adieu);
}
}
, SomeAPIRoutine , what(), , . , , adieu throw. , adieu Foo. : try-call Foo::bar, , () what().
, : ( ), if-, ""? , , . , (), ++ .