我执行了一个简单的 throw "TEST THROW",但它没有被我的 catch (std::exception& e) 捕获。这是因为我捕获的是 std::exception& e 吗?也就是说,只有从 std::exception 继承的异常类才会被捕获吗?如果不是,那么我是做错了什么,还是这是正常现象?顺便说一下,两个 catch 块都没有捕获到抛出的异常。
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;
}
std::exception
类型的对象,因此该异常不会被设计用于捕获std::exception
类型对象的 catch 子句所捕获。 - Igor Tandetnike
将是什么? - Raymond Chen