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;
}
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.
will not catch the
std::runtime_error*
you threw. It'll probably die with a call tostd::terminate
for the uncaught exception.Don't allocate the exception with
new
, just throw the constructor by-value, e.g.Since this is not a MCVE (what is
Core
?), I can't address explicitly the problem, but you are surely missing toActually, GCC compiles even without the inclusion, but exception won't be caught and you will end up with
You're throwing a
const char*
.std::exception
only catchesstd::exception
and all derived classes of it. So in order to catch your throw, you should throwstd::runtime_error("TEST THROW")
instead. Orstd::logic_error("TEST THROW")
; whatever fits better. The derived classes ofstd::exception
are listed here.You can add a catch (...) block to get it.
This also may happen when you throw an exception of a an inherted type but the inhertence is private