{"id":13732361,"url":"https://github.com/nlohmann/exception_reporter","last_synced_at":"2025-04-11T03:34:23.437Z","repository":{"id":145186536,"uuid":"200511376","full_name":"nlohmann/exception_reporter","owner":"nlohmann","description":"A tool to collect the exceptions that can reach a C++ function","archived":false,"fork":false,"pushed_at":"2019-08-04T15:46:57.000Z","size":11,"stargazers_count":24,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T01:34:08.802Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.reddit.com/r/cpp/comments/clss2i/how_to_find_all_exceptions_a_function_could_throw/","language":"Python","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/nlohmann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.MIT","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}},"created_at":"2019-08-04T15:44:49.000Z","updated_at":"2025-02-16T19:52:30.000Z","dependencies_parsed_at":"2024-01-31T04:26:01.171Z","dependency_job_id":null,"html_url":"https://github.com/nlohmann/exception_reporter","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/nlohmann%2Fexception_reporter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlohmann%2Fexception_reporter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlohmann%2Fexception_reporter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlohmann%2Fexception_reporter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nlohmann","download_url":"https://codeload.github.com/nlohmann/exception_reporter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248336783,"owners_count":21086875,"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":[],"created_at":"2024-08-03T02:01:54.125Z","updated_at":"2025-04-11T03:34:23.389Z","avatar_url":"https://github.com/nlohmann.png","language":"Python","funding_links":[],"categories":["Tools"],"sub_categories":[],"readme":"# Exceptions Reporter\n\nThis tool tries to answer this [r/cpp question](https://www.reddit.com/r/cpp/comments/clss2i/how_to_find_all_exceptions_a_function_could_throw/) for a tool to find out, for a given function in my code base, which exceptions it may encounter. It is a simple Python script processing a callgraph generated by Clang. It is far from being ready or useful, but demonstrates that this approach could be feasible.\n\nPlease open an issue for ideas or bugs.\n\n## Example\n\nAssume the following C++ program:\n\n```cpp\n#include \u003cmap\u003e\n\nusing int_map = std::map\u003cint, int\u003e;\n\nint bar(int i, const int_map\u0026 m)\n{\n    if (i == 100)\n    {\n        throw std::runtime_error(\"oops\");\n    }\n\n    try\n    {\n        return m.at(i);\n    }\n    catch (std::out_of_range\u0026)\n    {\n        return -1;\n    }\n}\n\nint main()\n{\n    int_map m;\n    m[1] = 1;\n\n    if (bar(2, m) == -1)\n    {\n        return 1;\n    }\n}\n```\n\nThe goal is to find out which exceptions can be encountered in its functions; that is, which `try`/`catch` block needs to be written.\n\n```sh\n# step 1: create a callgraph with clang and demangle its C++ symbols\nclang++ -S -emit-llvm examples/example.cpp -o - | opt -print-callgraph 2\u003e\u00261 | sed \"s/'/\\\"/g\" | c++filt -n \u003e callgraph.txt\n\n# step 2: analyze the callgraph\n./exeption_reporter.py callgraph.txt\n```\n\nFor the example, this is the output:\n\n```\nfunctions containing a throw:\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n- std::__1::__throw_length_error(char const*)\n- std::__1::__throw_out_of_range(char const*)\n\nfunctions that MAY throw std::logic_error:\n- std::__1::__throw_out_of_range(char const*)\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n- std::__1::__throw_length_error(char const*)\n\nfunctions that MAY throw std::length_error:\n- std::__1::__throw_length_error(char const*)\n\nfunctions that MAY throw std::out_of_range:\n- std::__1::__throw_out_of_range(char const*)\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n\nfunctions that MAY throw std::runtime_error:\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n\nfunctions that MAY encounter an exception:\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n- std::__1::__throw_length_error(char const*)\n- std::__1::__throw_out_of_range(char const*)\n- main\n- std::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e::allocate(unsigned long, void const*)\n- std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e::at(int const\u0026) const\n- std::__1::allocator_traits\u003cstd::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e \u003e::allocate(std::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e\u0026, unsigned long)\n- std::__1::unique_ptr\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e, std::__1::__tree_node_destructor\u003cstd::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e \u003e \u003e std::__1::__tree\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__map_value_compare\u003cint, std::__1::__value_type\u003cint, int\u003e, std::__1::less\u003cint\u003e, true\u003e, std::__1::allocator\u003cstd::__1::__value_type\u003cint, int\u003e \u003e \u003e::__construct_node\u003cstd::__1::piecewise_construct_t const\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e, std::__1::tuple\u003c\u003e \u003e(std::__1::piecewise_construct_t const\u0026\u0026\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e\u0026\u0026, std::__1::tuple\u003c\u003e\u0026\u0026)\n- std::__1::pair\u003cstd::__1::__tree_iterator\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e*, long\u003e, bool\u003e std::__1::__tree\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__map_value_compare\u003cint, std::__1::__value_type\u003cint, int\u003e, std::__1::less\u003cint\u003e, true\u003e, std::__1::allocator\u003cstd::__1::__value_type\u003cint, int\u003e \u003e \u003e::__emplace_unique_key_args\u003cint, std::__1::piecewise_construct_t const\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e, std::__1::tuple\u003c\u003e \u003e(int const\u0026, std::__1::piecewise_construct_t const\u0026\u0026\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e\u0026\u0026, std::__1::tuple\u003c\u003e\u0026\u0026)\n- std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e::operator[](int\u0026\u0026)\n\nfunctions that MAY encounter a std::logic_error exception:\n- std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e::at(int const\u0026) const\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n- std::__1::__throw_length_error(char const*)\n- std::__1::unique_ptr\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e, std::__1::__tree_node_destructor\u003cstd::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e \u003e \u003e std::__1::__tree\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__map_value_compare\u003cint, std::__1::__value_type\u003cint, int\u003e, std::__1::less\u003cint\u003e, true\u003e, std::__1::allocator\u003cstd::__1::__value_type\u003cint, int\u003e \u003e \u003e::__construct_node\u003cstd::__1::piecewise_construct_t const\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e, std::__1::tuple\u003c\u003e \u003e(std::__1::piecewise_construct_t const\u0026\u0026\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e\u0026\u0026, std::__1::tuple\u003c\u003e\u0026\u0026)\n- main\n- std::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e::allocate(unsigned long, void const*)\n- std::__1::__throw_out_of_range(char const*)\n- std::__1::allocator_traits\u003cstd::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e \u003e::allocate(std::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e\u0026, unsigned long)\n- std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e::operator[](int\u0026\u0026)\n- std::__1::pair\u003cstd::__1::__tree_iterator\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e*, long\u003e, bool\u003e std::__1::__tree\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__map_value_compare\u003cint, std::__1::__value_type\u003cint, int\u003e, std::__1::less\u003cint\u003e, true\u003e, std::__1::allocator\u003cstd::__1::__value_type\u003cint, int\u003e \u003e \u003e::__emplace_unique_key_args\u003cint, std::__1::piecewise_construct_t const\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e, std::__1::tuple\u003c\u003e \u003e(int const\u0026, std::__1::piecewise_construct_t const\u0026\u0026\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e\u0026\u0026, std::__1::tuple\u003c\u003e\u0026\u0026)\n\nfunctions that MAY encounter a std::length_error exception:\n- std::__1::__throw_length_error(char const*)\n- std::__1::unique_ptr\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e, std::__1::__tree_node_destructor\u003cstd::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e \u003e \u003e std::__1::__tree\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__map_value_compare\u003cint, std::__1::__value_type\u003cint, int\u003e, std::__1::less\u003cint\u003e, true\u003e, std::__1::allocator\u003cstd::__1::__value_type\u003cint, int\u003e \u003e \u003e::__construct_node\u003cstd::__1::piecewise_construct_t const\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e, std::__1::tuple\u003c\u003e \u003e(std::__1::piecewise_construct_t const\u0026\u0026\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e\u0026\u0026, std::__1::tuple\u003c\u003e\u0026\u0026)\n- main\n- std::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e::allocate(unsigned long, void const*)\n- std::__1::allocator_traits\u003cstd::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e \u003e::allocate(std::__1::allocator\u003cstd::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e \u003e\u0026, unsigned long)\n- std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e::operator[](int\u0026\u0026)\n- std::__1::pair\u003cstd::__1::__tree_iterator\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__tree_node\u003cstd::__1::__value_type\u003cint, int\u003e, void*\u003e*, long\u003e, bool\u003e std::__1::__tree\u003cstd::__1::__value_type\u003cint, int\u003e, std::__1::__map_value_compare\u003cint, std::__1::__value_type\u003cint, int\u003e, std::__1::less\u003cint\u003e, true\u003e, std::__1::allocator\u003cstd::__1::__value_type\u003cint, int\u003e \u003e \u003e::__emplace_unique_key_args\u003cint, std::__1::piecewise_construct_t const\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e, std::__1::tuple\u003c\u003e \u003e(int const\u0026, std::__1::piecewise_construct_t const\u0026\u0026\u0026, std::__1::tuple\u003cint\u0026\u0026\u003e\u0026\u0026, std::__1::tuple\u003c\u003e\u0026\u0026)\n\nfunctions that MAY encounter a std::out_of_range exception:\n- std::__1::__throw_out_of_range(char const*)\n- std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e::at(int const\u0026) const\n- main\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n\nfunctions that MAY encounter a std::runtime_error exception:\n- main\n- bar(int, std::__1::map\u003cint, int, std::__1::less\u003cint\u003e, std::__1::allocator\u003cstd::__1::pair\u003cint const, int\u003e \u003e \u003e const\u0026)\n```\n\n## License\n\nThe code is licensed under the [MIT License](http://opensource.org/licenses/MIT):\n\nCopyright \u0026copy; 2019 [Niels Lohmann](http://nlohmann.me)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlohmann%2Fexception_reporter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnlohmann%2Fexception_reporter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlohmann%2Fexception_reporter/lists"}