Giter VIP home page Giter VIP logo

mwe-cpp-exception's Introduction

Proper, portable exception handling with backtracing in C++11

Build Status

See GPMueller/trace for a proper exception backtracing library.

This MWE shows how std::nested_exception and std::throw_with_nested can be applied in order to not lose information while propagating an original std::exception upwards through a chain of function calls and create a backtrace without any overhead (compare e.g. logging of debug messages). This avoids much of the need for any debugging and provides a way of ensuring that a library does not crash ungracefully. Output should look something like this:

Library API: Exception caught in function 'api_function'
Backtrace:
~/Git/mwe-cpp-exception/src/detail/Library.cpp:17 : library_function failed
~/Git/mwe-cpp-exception/src/detail/Library.cpp:13 : could not open file "nonexistent.txt"

The example may seem a bit overkill, displaying a library with an API layer, but it shows a thorough way of dealing cleanly with exceptions. main calls an API function, which in turn calls a library function which deliberately throws. The API function catches the exception and calls a handler function.

Inspiration for this MWE was taken from https://stackoverflow.com/a/37227893/4069571 and https://stackoverflow.com/a/348862/4069571

Build

CMake is used to configure the build. To build the executable:

mkdir -p build
cd build
cmake ..
cmake --build .

TODO

  • Extend this MWE with example exceptions which do not require the code to terminate.
  • Create unit tests with catch, using e.g. REQUIRE_THROWS

mwe-cpp-exception's People

Contributors

gpmueller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mwe-cpp-exception's Issues

Won't compile

$ mkdir build; cd build; cmake ..; make

In file included from src/API.cpp:3:0:
include/detail/Exception.hpp:12:9: error: expected class-name before โ€˜{โ€™ token

$ gcc -v
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.