Exception not caught in try catch block

I am doing a simple "TEST THROW" throw and it does not get caught in my catch (std :: exception & e). Is it because I will catch std :: exception & e? I mean only caught the exception classes derived from std :: exception? If not, am I doing something wrong or is this normal? By the way, neither of the two catch blocks got caught in a throw exception.

int main()
{
try
{
    throw "TEST THROW"; // TEST
    Core core;

    core.Init();
    core.Load();

    while (!core.requestCloseWindow)
    {
        core.HandleInput();

        core.Update();
        core.Draw();
    }

    core.Unload();
    core.window->close();
}
catch (std::exception& e)
{
    std::cerr << e.what() << std::endl;
    try
    {
        time_t rawTime;
        struct tm* timeInfo;
        char timeBuffer [80];

        time(&rawTime);
        timeInfo = localtime(&rawTime);

        strftime(timeBuffer, 80, "%F %T", timeInfo);
        puts(timeBuffer);

        std::ofstream ofs; // Pas besoin de close, car le destructeur le fait.
        ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
        ofs.open("log.txt", std::ofstream::out | std::ofstream::app);
        ofs << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
        std::cerr << "An error occured while writing to a log file!" << std::endl;
    }
}

return 0;

      

}

+3


source to share


5 answers


You quit const char*

. std::exception

only catches std::exception

all of its derived classes. Therefore, in order to catch your throw, you must throw std::runtime_error("TEST THROW")

. Or std::logic_error("TEST THROW")

; which fits better. Derived classes std::exception

are listed here .



+4


source


Another reason people might run into this problem, especially if they've been writing Java recently, is because they might throw an exception pointer.

/* WARNING WARNING THIS CODE IS WRONG DO NOT COPY */
try {
    throw new std::runtime_error("catch me");
} catch (std::runtime_error &err) {
    std::cerr << "exception caught and ignored: " << err.what() << std::end;
}
/* WARNING WARNING THIS CODE IS WRONG DO NOT COPY */

      

will not catch std::runtime_error*

which one you threw. It will probably die calling std::terminate

for an uncaught exception.



Don't throw an exception with new

, just put in a constructor value for example.

try {
    /* note: no 'new' here */
    throw std::runtime_error("catch me");
} catch (std::runtime_error &err) {
    std::cerr << "exception caught and ignored: " << err.what() << std::end;
}

      

+2


source


You can add catch (...) to get it.

0


source


Since this is not an MCVE (what is it Core

?) I cannot explicitly state the problem, but you will surely miss

#include <exception>

      

Actually, GCC compiles even without including, but no exception will be caught and you will get

call completion after calling instance 'std :: exception'

what (): std :: exception

./ {program}: {PID} Canceled (kernel is reset)

0


source


This can also happen if you have thrown an exception from an inherited type, but the private property is private

0


source







All Articles