The intended use here is pretty simple. Although the C style of returning ints is ok (for C), it does has some drawbacks. I implemented a single Error class to allow me to return a full object and not just an int. After that, I worked with using templates so that I could return any object. Finally, I wanted do return any object with an exception.
Obtain the source via the download page.
Although the in-package documentation is slim, the classes are pretty simple and straight forward. The final versions are used via including TypedException.h (which includes TypedErrorMessage.h which includes ErrorMessage.cpp). The code was written and tested on Linux using g++ 4.1.2. There is a Makefile that should be sufficient to make the class and a simple test program. Simply ungzip and untar the distribution, cd into the src directory and type "make". The test programs for each version show simple example use... but only v7 or later is intended to be really useful.
try {
commandLine.process(argc, argv, "-b 1 -c 1 -d 1 -e 1 -h 0 -l 0 -n 2 -r 1");
} catch (TypedException & e) {
switch (e.code()) {
case CommandLine::InvalidParameter :
std::cout << "Error: (InvalidParameter) " << e << " " << std::endl;
exit(1);
break;
case CommandLine::RequiresParameter :
std::cout << "Error: (RequiresParameter) " << e << " " << std::endl;
exit(1);
break;
default :
std::cout << "Unknown exception(" << e << ")" << std::endl;
exit(1);
break;
}
}
which is triggered from lines such as:
throw TypedException(CommandLine::InvalidParameter, "can't convert '" + s + "' to int!");
Granted, this code uses an enum which is basically an int, but it is a type checked int. Also, a switch statement doesn't have to be used, so the return value from code() could be any object.