https://github.com/gpmueller/mwe-cpp-exception
Minimum working example of proper C++11 exception handling
https://github.com/gpmueller/mwe-cpp-exception
backtrace cpp11 exception-handler exception-handling exceptions mwe
Last synced: 28 days ago
JSON representation
Minimum working example of proper C++11 exception handling
- Host: GitHub
- URL: https://github.com/gpmueller/mwe-cpp-exception
- Owner: GPMueller
- License: mit
- Created: 2017-10-25T17:22:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-24T09:45:56.000Z (over 5 years ago)
- Last Synced: 2025-04-18T10:22:57.937Z (about 1 month ago)
- Topics: backtrace, cpp11, exception-handler, exception-handling, exceptions, mwe
- Language: C++
- Homepage: https://github.com/GPMueller/trace
- Size: 12.7 KB
- Stars: 28
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Proper, portable exception handling with backtracing in C++11
====================================================================[](https://travis-ci.org/GPMueller/mwe-cpp-exception)
**See [GPMueller/trace](https://github.com/GPMueller/trace) for a proper exception backtracing library.**
This MWE shows how [`std::nested_exception`](http://en.cppreference.com/w/cpp/error/nested_exception) and [`std::throw_with_nested`](http://en.cppreference.com/w/cpp/error/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`