C++ header-only library to easy the creation of exception types and provide a set of frequently used ones.
This library was created initially as part of the CalmPhotoFrame project within the PAIRG research group in UFRN (Federal University of Rio Grande do Norte). The code was extracted to allow the reuse by other projects.
-
Header-only (just include the header files)
-
Create new exception types with just one line Ex.: EXCEPTION_CLASS(MyAwesomeException);
-
Allows easy creation of exception hierarchies, to enable capture related exceptions without losing meaningful names. Ex.: EXCEPTION_CLASS(ClientError); EXCEPTION_SUBCLASS(NotFoundEntity, ClientError); EXCEPTION_SUBCLASS(UnauthorizedError, ClientError);
-
Includes the class name and (optionally) the cause of error on exception messages to help identify what kind of error was thrown Ex.: throw IOException("Could not read file");
Produces the message: "IOException: Could not read file"
-
Some useful macros (see macros.h) for checking exception conditions.
Ex.: CHECK_NOTNULL, CHECK_BOUNDS and CHECK_ASSERTION
Define 'COMMONEX_MACRO_CLASH' before including the library header if these macros are clashing with other names.
You can just copy the src/commonex dir into your project or you can use it with conan (a package manager for c/c++ projects).
The conan docs has detailed instructions in how to use it.
-
Install conan:
#You may need run this command as sudo pip install conan
-
Create a conanfile.txt in your project, like:
[requires] ... commonex/1.0.0@paulobrizolara/stable
or (if you prefer) use a conanfile.py:
from conans import ConanFile
class YourConanFileClass(ConanFile):
...
requires = (
... ,
"commonex/1.0.0@paulobrizolara/stable"
)
-
Install dependencies
conan install
-
Integrate it into your build
See conan integrations to learn how to integrate conan into your build.
- Start using!
Example:
...
#include commonex/commonex.h
EXCEPTION_CLASS(InvalidArgsException);
void run(int argc, char ** argv){
Obj * myObj = getObj();
CHECK_NOTNULL(myObj);
if(!myObj->init(argc, argv)){
throw InvalidArgsException("Could not initialize with given arguments");
}
}
int main(int argc, char ** argv){
try{
run(argc, argv);
}
catch(std::exception & ex){
std::cout << ex.what() << std::endl;
}
return 0;
}
The project is released under the MIT license (see LICENSE.txt).
If you see something wrong, ugly or incomplete you can make a pull request or open an issue.