Skip to content
/ commonex Public

C++ library to encapsulate common used exceptions

License

Notifications You must be signed in to change notification settings

p-brz/commonex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CommonEx

badge

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.

Features

  • 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.

How to use

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.

Quickstart with Conan

  1. Install conan:

    #You may need run this command as sudo pip install conan

  2. 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"
    )
  1. Install dependencies

    conan install

  2. Integrate it into your build

See conan integrations to learn how to integrate conan into your build.

  1. 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;
}

License and Contributions

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.

About

C++ library to encapsulate common used exceptions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published