Exceptional enlightenment

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

    #1

    Exceptional enlightenment

    I am puzzled by something. I have a program foo.cs it has
    a reference to an assembly bar.cs. The latter
    intentionally performs a divide by zero. The former has a
    try/catch pair for the offending method invocation which
    works in 2/3 of the cases I have tested. I am calling the
    bar.method() (which returns a string) as the first
    argument to MessageBox.show ():

    1) Running in the debugger - works
    2) Running from bin/Debug - does not (unhandled exception)
    3) Running from bin/Debug with bar.exe.config - works

    bar.exe.config contains:

    <system.windows .forms jitDebugging="t rue" />

    I suspect it is choking in the MessageBox.Show method. I
    can certainly work around it, but I would really like to
    know what is going on before adding a string object to do
    that.

    Can I get an expert opinion on this?

    Sorry if this is a repeat question

    Henry
  • Henry

    #2
    Exceptional enlightenment

    Oops I meant foo.exe.config.

    Comment

    • Michael Mayer

      #3
      Re: Exceptional enlightenment

      I don't fully understand exceptions on other threads / UI threads.. yet.
      I've been looking into it for windows forms applications. I'm not sure if
      this is applicable to your MessageBox.Show problem, but I had very similar
      results to you where it worked / didn't work.

      These are the two exception handler statements I've setup to catch unhandled
      exceptions for the entire app. (See example below).

      Application.Thr eadException += new
      System.Threadin g.ThreadExcepti onEventHandler
      (CatchThreadExc eption);
      AppDomain.Curre ntDomain.Unhand ledException += new
      UnhandledExcept ionEventHandler (CatchUnhandled Exception);


      --
      Mike Mayer

      mike@mag37.com

      Full test program...


      using System;
      using System.Drawing;
      using System.Collecti ons;
      using System.Componen tModel;
      using System.Windows. Forms;

      namespace UnhandledExcept ions
      {
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows. Forms.Form
      {
      private System.Windows. Forms.Button button1;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.Componen tModel.Containe r components = null;

      public Form1()
      {
      //
      // Required for Windows Form Designer support
      //
      InitializeCompo nent();

      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
      if( disposing )
      {
      if(components != null)
      {
      components.Disp ose();
      }
      }
      base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeCompo nent()
      {
      this.button1 = new System.Windows. Forms.Button();
      this.SuspendLay out();
      //
      // button1
      //
      this.button1.Lo cation = new System.Drawing. Point(88, 64);
      this.button1.Na me = "button1";
      this.button1.Ta bIndex = 0;
      this.button1.Te xt = "button1";
      this.button1.Cl ick += new
      System.EventHan dler(this.butto n1_Click);
      //
      // Form1
      //
      this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
      this.ClientSize = new System.Drawing. Size(292, 266);
      this.Controls.A ddRange(new System.Windows. Forms.Control[] {

      this.button1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayo ut(false);

      }
      #endregion

      private void button1_Click(o bject sender, System.EventArg s e)
      {
      BadMethod();
      }

      void BadMethod ()
      {
      throw new Exception ("Not a helpful message");
      }

      static void CaughtException (Exception e)
      {
      Console.WriteLi ne ("I caught the exception: ");
      if (e != null)
      {
      Console.WriteLi ne (e.ToString());
      }
      Console.ReadLin e();
      Console.WriteLi ne ("Cool");
      }

      static void CatchThreadExce ption (
      object sender,
      System.Threadin g.ThreadExcepti onEventArgs e
      )
      {
      CaughtException (e.Exception);
      }


      static void CatchUnhandledE xception(
      object sender,
      UnhandledExcept ionEventArgs e
      )
      {
      CaughtException (e.ExceptionObj ect as Exception);
      }


      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
      Application.Thr eadException += new
      System.Threadin g.ThreadExcepti onEventHandler
      (CatchThreadExc eption);
      AppDomain.Curre ntDomain.Unhand ledException += new
      UnhandledExcept ionEventHandler
      (CatchUnhandled Exception);

      Form1 form1 = new Form1();
      Application.Run (form1);

      }



      }
      }


      Comment

      • Michael Mayer

        #4
        Re: Exceptional enlightenment

        Here's an article that talks about exceptions in different types of apps.

        http://www.wintellect.com/resources/...s/appsdie.aspx

        --
        Mike Mayer

        mike@mag37.com


        "Henry" <hsigy@yahoo.co m> wrote in message
        news:064301c37a 56$82144b10$a00 1280a@phx.gbl.. .[color=blue]
        > I am puzzled by something. I have a program foo.cs it has
        > a reference to an assembly bar.cs. The latter
        > intentionally performs a divide by zero. The former has a
        > try/catch pair for the offending method invocation which
        > works in 2/3 of the cases I have tested. I am calling the
        > bar.method() (which returns a string) as the first
        > argument to MessageBox.show ():
        >
        > 1) Running in the debugger - works
        > 2) Running from bin/Debug - does not (unhandled exception)
        > 3) Running from bin/Debug with bar.exe.config - works
        >
        > bar.exe.config contains:
        >
        > <system.windows .forms jitDebugging="t rue" />
        >
        > I suspect it is choking in the MessageBox.Show method. I
        > can certainly work around it, but I would really like to
        > know what is going on before adding a string object to do
        > that.
        >
        > Can I get an expert opinion on this?
        >
        > Sorry if this is a repeat question
        >
        > Henry[/color]


        Comment

        • Henry

          #5
          Re: Exceptional enlightenment


          Thanks Mike:

          The article states "In the application configuration
          file, add a system.windows. forms element under the
          configuration element and set the jitDebugging attribute
          to true. Now you'll get the JIT dialog instead of the
          normal exception."

          My trouble is that I don't get the JIT dialog; instead my
          app works fine handling the exception as expected. I am
          calling the offending method from fooButton_Click () and
          wonder if that may be the cause. It is the nonorthogonal
          nature of the problem that has me scratching my head.

          Henry

          Comment

          • Henry

            #6
            Re: Exceptional enlightenment

            Thanks again Mike:

            This does look quite a bit like my problem. I will give
            it a try tomorrow.

            Henry

            Comment

            • Henry

              #7
              Re: Exceptional enlightenment

              Thank you Mike. That was indeed the problem. Now I just
              need to figure out why it works. One of my books alludes
              to the possibility that this might be a bug in the CLR.

              Henry



              Comment

              • William Stacey

                #8
                Re: Exceptional enlightenment

                So in this example "CatchUnhandled Exception" is just a standard exception
                class that you created? Thanks for this example.

                --
                William Stacey, DNS MVP

                "Michael Mayer" <mike@mag37.com > wrote in message
                news:OFAJEYmeDH A.3228@tk2msftn gp13.phx.gbl...[color=blue]
                > I don't fully understand exceptions on other threads / UI threads.. yet.
                > I've been looking into it for windows forms applications. I'm not sure if
                > this is applicable to your MessageBox.Show problem, but I had very similar
                > results to you where it worked / didn't work.
                >
                > These are the two exception handler statements I've setup to catch[/color]
                unhandled[color=blue]
                > exceptions for the entire app. (See example below).
                >
                > Application.Thr eadException += new
                > System.Threadin g.ThreadExcepti onEventHandler
                > (CatchThreadExc eption);
                > AppDomain.Curre ntDomain.Unhand ledException += new
                > UnhandledExcept ionEventHandler (CatchUnhandled Exception);
                >
                >
                > --
                > Mike Mayer
                > http://www.mag37.com/csharp/
                > mike@mag37.com
                >
                > Full test program...
                >
                >
                > using System;
                > using System.Drawing;
                > using System.Collecti ons;
                > using System.Componen tModel;
                > using System.Windows. Forms;
                >
                > namespace UnhandledExcept ions
                > {
                > /// <summary>
                > /// Summary description for Form1.
                > /// </summary>
                > public class Form1 : System.Windows. Forms.Form
                > {
                > private System.Windows. Forms.Button button1;
                > /// <summary>
                > /// Required designer variable.
                > /// </summary>
                > private System.Componen tModel.Containe r components = null;
                >
                > public Form1()
                > {
                > //
                > // Required for Windows Form Designer support
                > //
                > InitializeCompo nent();
                >
                > }
                >
                > /// <summary>
                > /// Clean up any resources being used.
                > /// </summary>
                > protected override void Dispose( bool disposing )
                > {
                > if( disposing )
                > {
                > if(components != null)
                > {
                > components.Disp ose();
                > }
                > }
                > base.Dispose( disposing );
                > }
                >
                > #region Windows Form Designer generated code
                > /// <summary>
                > /// Required method for Designer support - do not modify
                > /// the contents of this method with the code editor.
                > /// </summary>
                > private void InitializeCompo nent()
                > {
                > this.button1 = new System.Windows. Forms.Button();
                > this.SuspendLay out();
                > //
                > // button1
                > //
                > this.button1.Lo cation = new System.Drawing. Point(88, 64);
                > this.button1.Na me = "button1";
                > this.button1.Ta bIndex = 0;
                > this.button1.Te xt = "button1";
                > this.button1.Cl ick += new
                > System.EventHan dler(this.butto n1_Click);
                > //
                > // Form1
                > //
                > this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
                > this.ClientSize = new System.Drawing. Size(292, 266);
                > this.Controls.A ddRange(new System.Windows. Forms.Control[] {
                >
                > this.button1});
                > this.Name = "Form1";
                > this.Text = "Form1";
                > this.ResumeLayo ut(false);
                >
                > }
                > #endregion
                >
                > private void button1_Click(o bject sender, System.EventArg s e)
                > {
                > BadMethod();
                > }
                >
                > void BadMethod ()
                > {
                > throw new Exception ("Not a helpful message");
                > }
                >
                > static void CaughtException (Exception e)
                > {
                > Console.WriteLi ne ("I caught the exception: ");
                > if (e != null)
                > {
                > Console.WriteLi ne (e.ToString());
                > }
                > Console.ReadLin e();
                > Console.WriteLi ne ("Cool");
                > }
                >
                > static void CatchThreadExce ption (
                > object sender,
                > System.Threadin g.ThreadExcepti onEventArgs e
                > )
                > {
                > CaughtException (e.Exception);
                > }
                >
                >
                > static void CatchUnhandledE xception(
                > object sender,
                > UnhandledExcept ionEventArgs e
                > )
                > {
                > CaughtException (e.ExceptionObj ect as Exception);
                > }
                >
                >
                > /// <summary>
                > /// The main entry point for the application.
                > /// </summary>
                > [STAThread]
                > static void Main(string[] args)
                > {
                > Application.Thr eadException += new
                > System.Threadin g.ThreadExcepti onEventHandler
                > (CatchThreadExc eption);
                > AppDomain.Curre ntDomain.Unhand ledException += new
                > UnhandledExcept ionEventHandler
                > (CatchUnhandled Exception);
                >
                > Form1 form1 = new Form1();
                > Application.Run (form1);
                >
                > }
                >
                >
                >
                > }
                > }
                >
                >[/color]


                Comment

                • Michael Mayer

                  #9
                  Re: Exceptional enlightenment

                  "William Stacey" <staceyw@mvps.o rg> wrote in message
                  news:esdVGrueDH A.1888@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > So in this example "CatchUnhandled Exception" is just a standard exception
                  > class that you created? Thanks for this example.[/color]

                  No, it's a method with the correct signature for the
                  UnhandledExcept ionEventHandler delegate. The full code was listed at the
                  bottom of my previous message.

                  Unfortunately, the ThreadException EventHandler and the
                  UnhandledExcept ionEventHandler have different signatures. So I ended up
                  with two methods - one for each delegate. Each one simply called this
                  method:

                  CaughtException (Exception e)

                  which in my "real apps" would be a dialog to notify the user and/or logging
                  logic to store the error in the event log, a file, etc. This is where I
                  would plugin Microsoft's Exception Management Application Block:

                  http://msdn.microsoft.com/library/de...asp?frame=true


                  Mike Mayer

                  mike@mag37.com


                  Comment

                  • Henry

                    #10
                    Re: Exceptional enlightenment

                    I just want to follow up on this adventure. After Mike so
                    kindly put me on the right path, I looked up
                    ThreadException EventHandler in Applied Microsoft .NET
                    Framework Prgramming - Jeffrey Richter, and I found a
                    cogent description on Pages 432-440. In particular the
                    following passage summed up what I was seeing.

                    "As I explained, the behavior of NativeWindows internal
                    method is to catch all CLS-compliant exceptions and
                    display a dialog box or call any callback method that
                    you've registered with the ThreadException event.
                    However, some situations can change this default
                    behavior. First, if a debugger is attached to your
                    Windows Forms application, any unhandled exception is
                    allowed to propogate up the call stack. Second, if a JIT
                    debugger is installed and if the jitDebugging
                    configuration setting is specified in the application's
                    XML .config file, then again, the exception isn't caught
                    and the exception is allowed to propogate up the
                    callstack"

                    Now I get it. This does seem a bit arcane for a
                    technology which is supposed to reduce development time.
                    Furthermore, it would seem to be rich a source of hidden
                    bugs which QA or customers might find instead of the
                    deveopers.

                    Thanks again Mike.

                    Henry

                    Comment

                    Working...