Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception not caught in try catch block

Tags:

c++

exception

I do a simple throw "TEST THROW" and it isn't caught in my catch (std::exception& e). Is it because I'm catching an std::exception& e? I mean, are only exception classes derived from std::exception caught? If not, am I doing something wrong or is it normal? By the way, none of the two catch blocks caught the 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;

}

like image 597
Benoît Dubreuil Avatar asked May 31 '15 17:05

Benoît Dubreuil


People also ask

What happens if exception is not caught in try catch?

If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

Which exception Cannot be caught in Java?

RuntimeException, it's an unchecked exception so you don't have to catch it.

What happens if exception is not handled in Java?

if you don't handle exceptions When an exception occurred, if you don't handle it, the program terminates abruptly and the code past the line that caused the exception will not get executed.

Is Catch mandatory for TRY block?

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.


1 Answers

Another reason people may hit this issue, especially if they've been writing Java recently, is that they may be throwing a pointer to the exception.

/* 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 the std::runtime_error* you threw. It'll probably die with a call to std::terminate for the uncaught exception.

Don't allocate the exception with new, just throw the constructor by-value, e.g.

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;
}
like image 134
Craig Ringer Avatar answered Sep 23 '22 19:09

Craig Ringer