{"id":15574347,"url":"https://github.com/gpmueller/mwe-cpp-exception","last_synced_at":"2025-04-24T02:11:41.536Z","repository":{"id":152302592,"uuid":"108301964","full_name":"GPMueller/mwe-cpp-exception","owner":"GPMueller","description":"Minimum working example of proper C++11 exception handling","archived":false,"fork":false,"pushed_at":"2019-12-24T09:45:56.000Z","size":13,"stargazers_count":28,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T10:22:57.937Z","etag":null,"topics":["backtrace","cpp11","exception-handler","exception-handling","exceptions","mwe"],"latest_commit_sha":null,"homepage":"https://github.com/GPMueller/trace","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GPMueller.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-10-25T17:22:05.000Z","updated_at":"2025-03-14T09:56:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"07f5ad1f-aa4e-4899-8c7c-23b71b86ed33","html_url":"https://github.com/GPMueller/mwe-cpp-exception","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Fmwe-cpp-exception","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Fmwe-cpp-exception/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Fmwe-cpp-exception/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GPMueller%2Fmwe-cpp-exception/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GPMueller","download_url":"https://codeload.github.com/GPMueller/mwe-cpp-exception/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250546086,"owners_count":21448260,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backtrace","cpp11","exception-handler","exception-handling","exceptions","mwe"],"created_at":"2024-10-02T18:16:42.182Z","updated_at":"2025-04-24T02:11:41.530Z","avatar_url":"https://github.com/GPMueller.png","language":"C++","readme":"Proper, portable exception handling with backtracing in C++11\n====================================================================\n\n[![Build Status](https://api.travis-ci.org/GPMueller/mwe-cpp-exception.svg?branch=master)](https://travis-ci.org/GPMueller/mwe-cpp-exception)\n\n**See [GPMueller/trace](https://github.com/GPMueller/trace) for a proper exception backtracing library.**\n\nThis 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\nan original `std::exception` upwards through a chain of function calls and create a **backtrace** without any overhead (compare e.g. logging of debug messages).\nThis avoids much of the need for any debugging and provides a way of ensuring that a library does not crash ungracefully.\nOutput should look something like this:\n```\nLibrary API: Exception caught in function 'api_function'\nBacktrace:\n~/Git/mwe-cpp-exception/src/detail/Library.cpp:17 : library_function failed\n~/Git/mwe-cpp-exception/src/detail/Library.cpp:13 : could not open file \"nonexistent.txt\"\n```\n\nThe example may seem a bit overkill, displaying a library with an API layer, but it shows a thorough way of dealing cleanly with exceptions.\n`main` calls an API function, which in turn calls a library function which deliberately throws. The API function catches the exception and\ncalls a handler function.\n\nInspiration for this MWE was taken from https://stackoverflow.com/a/37227893/4069571 and https://stackoverflow.com/a/348862/4069571\n\nBuild\n--------------------------------------------------------------------\n\nCMake is used to configure the build. To build the executable:\n```\nmkdir -p build\ncd build\ncmake ..\ncmake --build .\n```\n\nTODO\n--------------------------------------------------------------------\n- Extend this MWE with example exceptions which do not require the code to terminate.\n- Create unit tests with catch, using e.g. `REQUIRE_THROWS`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpmueller%2Fmwe-cpp-exception","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgpmueller%2Fmwe-cpp-exception","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpmueller%2Fmwe-cpp-exception/lists"}