How to create an UnhandledException?

I use this code to catch a WinForm UnhandledException application.

[STAThread]
static void Main(string[] args)
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new
      System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

    // 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(CurrentDomain_UnhandledException);

    try
    {
        Application.Run(new MainForm());
    } catch....

There I will try to restart the application. Now my problem is to simulate an exception like this. I tried before trying (mostly): throw new NullReferenceException("test");VS caught it.

I also tried in the MainForm code with a button:

    private void button1_Click(object sender, EventArgs ev)
    {         
        ThreadPool.QueueUserWorkItem(new WaitCallback(TestMe), null);
    }

    protected void TestMe(object state)
    {
        string s = state.ToString();
    }

didn't help, VS caught it, even in Release mode.

  • How should I finally get the application to generate ? UnhandleldException
  • Will I be able to restart the application in CurrentDomain_UnhandledException?
  • How can i create ? ThreadException

PS.

If I run out VS window with a common window

MyApplication application "encountered an error and needs to close ... blabla ... Send Report / Do Not Send.

I want, however, VS introduce this method (... Domain_Unhahdled ...)

EDIT: Windows, : alt text http://byfiles.storage.msn.com/y1pOhWnAAXfMYtJH2VNa5iL0l1hjAqNHD2VmVnl8nN6L1oQC_xHkyHCJxhMc1ZLxLOH9ZXfZoo5zX8?PARTNER=WRITER

:

static void CurrentDomain_UnhandledException(object sender, 
    UnhandledExceptionEventArgs e)
{
    // Since we can't prevent the app from terminating
    // log this to the event log.
    Logger.LogMessage(ERROR, errorMsg);
    Application.Restart();
+3
2

, , , :

  • Application.ThreadException Application.SetUnhandledExceptionMode Windows Forms. ThreadPool Windows Forms, Winforms, .

  • AppDomain.CurrentDomain.UnhandledException , ThreadPool.

  • UnhandledException . , , , ( .NET 1.1, ... .) .

, , , , "" . , .

: , .

, , :

  • . , , , , . AppDomain.UnhandledException . , , . .

    , , , . , , , . , , , . , " " - .

  • Windows . , , ( , , , ..), . , API .NET. , , , .

  • , , . - , , .

    , , WAIT_ABANDONED (AbandonedMutexException .NET). , , , , .. . .

. , , , , . , 't , , , .

, , " ", , , . - . , , . , , , -, - , - .

, , - : " , , , . , ". , , ( " ", , , , ).

, . - , .

+6

Visual Studio , , . VS.

- , ( ) . , , , . -, . .

0

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


All Articles