I experienced some unwanted appcrashs, when windows (7 or 10) forces my application to close on shutdown.
Using the documentation and older posts, I came up with the following code:
Code: Select all
EVT_END_SESSION(MyApp::OnSystemShutdown)
EVT_QUERY_END_SESSION(MyApp::OnSystemEndSession)
bool MyApp::OnExceptionInMainLoop()
{
try
{
throw;
}
catch (const wxString & str)
{
DoLog("OnExceptionInMainLoop() error: " + str, true);
throw;
}
catch (const std::exception& e)
{
wxString sErr = "";
sErr << "OnExceptionInMainLoop() error: " << e.what();
DoLog(sErr, true);
throw;
}
catch (...)
{
DoLog("OnExceptionInMainLoop unknown error", true);
throw;
}
return true;
}
void MyApp::OnUnhandledException()
{
wxString sErr = "";
sErr << "Unhandled Exception thrown, wxSysErrorCode: " << wxSysErrorCode();
DoLog(sErr, true);
}
void MyApp::DoLog(wxString logstring, bool bForceLog)
{
bool bLogging = false;
if (bForceLog)
{
bLogging = true;
}
else if (mb_writelogfile)
{
bLogging = true;
}
if(bLogging)
{
wxLogNull logNo;
wxFileName fn_logfile = wxStandardPaths::Get().GetExecutablePath();
fn_logfile.SetExt("log");
wxString datetime = wxDateTime::Now().Format();
wxString buf = "";
buf << datetime << " - " << logstring;
wxTextFile file(fn_logfile.GetFullPath());
if(!fn_logfile.FileExists())
{
file.Create();
}
if(file.Open())
{
while(file.GetLineCount()>500)
{
file.RemoveLine(0);
}
file.AddLine(buf);
/*Datei speichern*/
file.Write();
file.Close();
}
}
}
Furthermore I noticed, that a the error message on a appcrash in normal windows running mode is also caught, but I get no logging message. It just results in a silent closing of my app.
I tried other ways of displaying a message in this case to no avail (eg. wxMessageBox, wxMeesageDialog) so im not even sure, if the end process is even running through this routine. All im noticing is, that if I handle exceptions of the main loop myself, I get no standard error messages whatsoever.
In fact, the only way to get a proper message out was when I provoked a std::exception in a try catch block and threw that myself.
Am I missing something here? Do I need to catch the exception in every class and if so, what would be the function for that?
Or do I really need to place every function in a try catch block to get proper exception handling?
I am fine with a message, when windows is running and I want to suppress messages, when windows is shutting down.
Thank you.
Greets
Natu