Skip to content

Universal C++ Exception Library with StackTrace Builtin and Unicode Text Support

License

Notifications You must be signed in to change notification settings

voldien/exceptcxx

Repository files navigation

ExecptCXX

ExceptCxx License: MIT GitHub release

A simple library for adding universal exceptions more additional meta than the common std::exception while preserving the original code when integrating.

Features

  • StackTrace - Support to get the stack tree of when it was invoked.
  • Unicode - Allow support for both ASCII, Unicode 8, and unicode 16.
  • Backward compadible with standard exception.

Motivation

Attempt to create a dedicated Exception library with useful debug info and stack trace info while maintaining the standard c++ exception. Allowing to reuse the code in multiple projects with a good level of information when the exception is thrown.

Installation

The library can be built simply by the following commands.

mkdir build
cd build
cmake ..
make

A note, this exception library is using external submodule git. Use the following command to download all the dependent git repositories.

git submodule update --init --recursive 

Integration with CMake

The idea is to be able to integrate this library with another project easily. With CMake, it basically requires 2 lines. One for adding the project and the second for adding it as a dependent linked library target.

ADD_SUBDIRECTORY(exceptCXX EXCLUDE_FROM_ALL)
TARGET_LINK_LIBRARIES(myTarget PUBLIC cxxexcept)

Dependencies

The dependices currently is related to backward-cpp

apt install binutils-dev

Examples

The following is a simple example for throwing an exception, followed by printing a formated error message to stdout.

try {
    throw cxxexcept::DivideByZeroException();
} catch (const std::exception &ex) {
    cxxexcept::printStackMessage(ex);
}

Another example, using a more common exception type, runtime exception.

try {
    throw cxxexcept::RuntimeException();
} catch (const std::exception &ex) {
     cxxexcept::printStackMessage(ex);
}

Getting a comprehensive string of both the stack as well the cause of exception can be extracted with the following method.

std::cerr << cxxexcept::getStackMessage(ex);

License

This project is licensed under the MIT License - see the LICENSE file for details