Is there a parameter that prevents the unhandled exception from being displayed in the applications I'm compiling?

I'm not sure this is the right place to ask, but I do not care.

I have 2 .NET applications; the one that I compiled is not the other. Both use the .NET Framework 4.5. But they handle exceptions differently.

In an application that I did NOT compile; it shows the raw exception dialog (which I want and expect)

Unhandled Exception from app I didnt compile

But in the application I compiled; it just shows that the application crashed,

Unhandled Exepction from app I compiled

So, should there be a setting in the VS configuration or project configuration that prevents the unhandled exception from being displayed in the applications that I am compiling? ...

I tried reinstalling VS by changing the settings in the Debug-> Exceptions menu, and it didn't work ...

+6
source share
2 answers

The top screenshot is ThreadExceptionDialog. It is displayed in the most specific case when the Winforms application is bombed in the event handler that was launched by the message loop (Application.Run), and the application otherwise did not reassign the Application.ThreadException event handler. Using it is not a good practice, there is no reasonable way in which the user could know whether to click the Continue or Exit button. Be sure to call Application.SetUnhandledExceptionMode () to disable it.

The bottom screenshot is the default Windows Error Reporting dialog box displayed by Windows when the program is bombarded with an unhandled exception. You should never allow this to come to this, the dialog box does not display enough information to help someone diagnose and fix the problem. Always write an event handler for the AppDomain.CurrentDomain.UnhandledException event. Display and / or write e.ExceptionObject.ToString () and call Environment.Exit () to terminate the application.

Make your Program.cs source code look like this:

[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.Run(new Form1()); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { // TODO: improve logging and reporting MessageBox.Show(e.ExceptionObject.ToString()); Environment.Exit(-1); } 
+11
source

The solution presented by Hans Passant will cease to apply. It also means that the remaining finally blocks are no longer running.

You can also use PInvoke to change the behavior by calling the SetErrorMode() method.

 public enum ErrorModes : uint { SYSTEM_DEFAULT = 0x0, SEM_FAILCRITICALERRORS = 0x0001, SEM_NOALIGNMENTFAULTEXCEPT = 0x0004, SEM_NOGPFAULTERRORBOX = 0x0002, SEM_NOOPENFILEERRORBOX = 0x8000, SEM_NONE = SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX } [DllImport("kernel32.dll")] static extern ErrorModes SetErrorMode(ErrorModes uMode); 

and then call

 SetErrorMode(ErrorModes.SEM_NONE); 

Doing this will finally enable blocking the launch.

+2
source

Source: https://habr.com/ru/post/921019/


All Articles