Can C# WinForm static void Main NOT catching Exception? -


i have winform application written in c# put try-catch block in program.cs, in program entry, static void main method, right in beginning of application this:

using system; using system.io; using system.windows.forms;  namespace t5shortesttime {     static class program {         /// <summary>         /// main entry point application.         /// </summary>         [stathread]         static void main() {             try {                 application.enablevisualstyles();                 application.setcompatibletextrenderingdefault(false);                 application.run(new t5shortesttimeform());             } catch (exception e) {                 string errordir = path.combine(application.startuppath, "errorlog");                 string errorlog = path.combine(errordir, datetime.now.tostring("yyyymmdd_hhmmss_fff") + ".txt");                 if (!directory.exists(errordir))                     directory.createdirectory(errordir);                                 file.writealltext(errorlog, e.tostring());                           }         }     } } 

as can see, application put in try-catch block , in catch block, thing create error log file.

now, far good. application running , if encounter crash, last exception should captured try-catch block , stored in error log file.

however, run program while, unhandled exception (null reference). surprise me exception not create error log file.

now, this post shows possibly caused threadexception or handleprocesscorruptedstateexceptions (the 2 upvoted answers), case shows simple null reference exception:

problem signature:   problem event name:   clr20r3   problem signature 01: t5shortesttime.exe   problem signature 02: 2.8.3.1   problem signature 03: 5743e646   problem signature 04: t5shortesttime   problem signature 05: 2.8.3.1   problem signature 06: 5743e646   problem signature 07: 182   problem signature 08: 1b   problem signature 09: system.nullreferenceexception   os version:   6.3.9600.2.0.0.272.7   locale id:    1033   additional information 1: bb91   additional information 2: bb91a371df830534902ec94577ebb4a3   additional information 3: aba1   additional information 4: aba1ed7202d796d19b974eec93d89ec2  read our privacy statement online:   http://go.microsoft.com/fwlink/?linkid=280262  if online privacy statement not available, please read our privacy statement offline:   c:\windows\system32\en-us\erofflps.txt 

why be?

the last exception should captured try-catch block

that not going happen. except in 1 case, when run program debugger attached. surely got lulled believing work, starts out running program f5 while.

application.run() has back-stop in code raises events, try/catch-em-all raises application.threadexception event when event handler throws unhandled exception. back-stop really, really necessary, on x64 version of windows 7. very bad things happen when there no exception handler. back-stop not in place when run debugger, makes unhandled exceptions difficult debug.

so when debug catch clause run. making unhandled exceptions difficult debug. when run without debugger catch clause not run , program crash, described. making unhandled exception difficult debug.

so don't way. how application.run() deals unhandled exceptions configured application.setunhandledexceptionmode() method. you'll version better:

    [stathread]     static void main() {         application.enablevisualstyles();         application.setcompatibletextrenderingdefault(false);         if (!system.diagnostics.debugger.isattached) {             application.setunhandledexceptionmode(unhandledexceptionmode.throwexception);             appdomain.currentdomain.unhandledexception += logexception;         }         application.run(new form1());     }      private static void logexception(object sender, unhandledexceptioneventargs e) {         string errordir = path.combine(application.startuppath, "errorlog");         string errorlog = path.combine(errordir, datetime.now.tostring("yyyymmdd_hhmmss_fff") + ".txt");         if (!directory.exists(errordir))             directory.createdirectory(errordir);         file.writealltext(errorlog, e.tostring());         appdomain.currentdomain.unhandledexception -= logexception;         messagebox.show("error details recorded in " + errorlog, "unexpected error");         environment.exit(1);     } 

with code in place, can debug unhandled exceptions without problems. debugger.isattached test ensures debugger stop when event handler falls over. without debugger, disables application.threadexception event (it quite useless) , favors listening all exceptions. including ones raised in worker threads.

you ought give alert user window doesn't disappear without trace. going recommend messagebox noticed this bug again on windows 10. sigh.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -