How to use ThreadException?

I tried to use

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399

but when i do it

throw new ArgumentNullException("playlist is empty");

I get nothing. I bet I miss something very obvious.

here is my code.

using System;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Threading;

namespace MediaPlayer.NET
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
        private static void Main()
        {
            // Add the event handler for handling UI thread exceptions to the event.
            Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);

            // Set the unhandled exception mode to force all Windows Forms errors to go through
            // our handler.
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            // Add the event handler for handling non-UI thread exceptions to the event. 
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(UnhandledException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MediaPlayerForm());
        }

        private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("UnhandledException!!!!");
        }

        private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
        {
            MessageBox.Show("UIThreadException!!!!",
                            "UIThreadException!!!!", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
            Application.Exit();
        }
    }
}
+3
source share
2 answers

, , . , Windows SEH 32- 64- Windows 8. , - , Load event OnLoad(). , Project + Properties, Build, Platform Target = AnyCPU, unick " 32-", .

, , Application.ThreadException . , :

#if (!DEBUG)
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
#endif

ThreadException, AppDomain.UnhandledException. #if - , , .

UnhandledException, Windows:

        Environment.Exit(1);
+9

, ArgumentNullException. , MediaPlayerForm ( ), . Application.ThreadException , Windows Forms, .

+2

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


All Articles