Application unhandled exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shehab Kamal

    #1

    Application unhandled exception

    In Main I register ThreadException to handle unhandled exceptions
    Application.Thr eadException += new
    ThreadException EventHandler(Ap p_ThreadExcepti on);
    The method will show a MessageBox or a Form instead of .NET 2 default
    dialog. However I don't get the code work. What am I missing?

    One silly question. Can I post .NET 2 questions here or should I post to
    msdn2?

    Thanks in advance,
    Shehab.
  • Barry Kelly

    #2
    Re: Application unhandled exception

    Shehab Kamal <ShehabKamal@di scussions.micro soft.comwrote:
    In Main I register ThreadException to handle unhandled exceptions
    Application.Thr eadException += new
    ThreadException EventHandler(Ap p_ThreadExcepti on);
    The method will show a MessageBox or a Form instead of .NET 2 default
    dialog. However I don't get the code work. What am I missing?
    I don't know. You need to run outside the debugger for one thing,
    because under the debugger VS hacks your application message loop and
    inserts its own code into the call stack - highly frustrating, I
    personally find. Also, it will only catch exceptions that are invoked on
    the GUI thread via message loop dispatching - things like painting,
    click events, etc.

    This console application works well for me:

    ---8<---
    using System;
    using System.Windows. Forms;

    static class App
    {
    static void Main()
    {
    Application.Thr eadException += delegate
    {
    Console.WriteLi ne("Caught!");
    };

    Form form = new Form();
    form.Click += delegate
    {
    throw new Exception();
    };

    Application.Run (form);
    }
    }
    --->8---
    One silly question. Can I post .NET 2 questions here or should I post to
    msdn2?
    ..NET 2 is .NET as well.

    -- Barry

    --

    Comment

    • Michael Nemtsev

      #3
      Re: Application unhandled exception

      Hello Shehab,

      Add event handler for all threads in the appdomain

      appdomain.Curre ntDomain.Unhand ledException += new UnhandledExcept ionEventHandler (CurrentDomain_ UnhandledExcept ion);


      SKIn Main I register ThreadException to handle unhandled exceptions
      SKApplication.T hreadException += new
      SKThreadExcepti onEventHandler( App_ThreadExcep tion);
      SKThe method will show a MessageBox or a Form instead of .NET 2
      SKdefault
      SKdialog. However I don't get the code work. What am I missing?
      SKOne silly question. Can I post .NET 2 questions here or should I
      SKpost to msdn2?
      SK>
      SKThanks in advance,
      SKShehab.
      ---
      WBR,
      Michael Nemtsev :: blog: http://spaces.msn.com/laflour

      "At times one remains faithful to a cause only because its opponents do not
      cease to be insipid." (c) Friedrich Nietzsche


      Comment

      • Shehab Kamal

        #4
        Re: Application unhandled exception

        The 2 answers complete each other.
        I have thrown an excpetion after clicking a button and it was handled by App
        not AppDomain.
        I have thrown an exception from the constructor and it was handled by the
        AppDomain not App. However the "sorry for inconvenience" message box appears,
        can I get rid of it or it is built in windows?

        Thanks for the 2 answers.

        Comment

        Working...