c++ - Exception not caught in try catch block -


i simple throw "test throw" , isn't caught in catch (std::exception& e). because i'm catching std::exception& e? mean, exception classes derived std::exception caught? if not, doing wrong or normal? way, none of 2 catch blocks caught 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 log file!" << std::endl;     } }  return 0; 

}

you're throwing const char*. std::exception catches std::exception , derived classes of it. in order catch throw, should throw std::runtime_error("test throw") instead. or std::logic_error("test throw"); whatever fits better. derived classes of std::exception listed here.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -