samedi 13 janvier 2024

C++: how to locate the exception?

I have the following test code.

#include <exception>
#include <iostream>

void terminate_handler() {
    auto const ep = std::current_exception();
    if(ep) {
        try {
            std::rethrow_exception(ep);
        } catch(const std::logic_error& le) {
            std::cerr << "terminating with std::logic_error: " << le.what()
                      << std::endl;
        }
    }
    std::abort();
}

int main(int argc, char* argv[]) {
    std::set_terminate(terminate_handler);
    std::string(0);
    return 0;
}

It is working fine, but I want to add filename and line number like below if possible.

terminating with std::logic_error: basic_string::_M_construct null not valid at main.cpp:26.

Aucun commentaire:

Enregistrer un commentaire