Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. exception not catching

exception not catching

Scheduled Pinned Locked Moved C#
helpvisual-studiodebugging
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rob Tomson
    wrote on last edited by
    #1

    I'm trying to implement an error report in my program so that when it crashes it pops up a dialog that allows you to fill in some information and then send it along through email. The problem that I'm having is when I debug the program in VS it crashes gracefully, just like it's supposed to. But when I run it as a standalone .exe it just errors as if there is no try/catch statement. This is what I have for my Main function. If there are any suggestions please let me know. static void Main() { try { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.Run(new frmMDI()); } catch(System.Exception e) { System.Windows.Forms.Application.Run(new frmErrorReport(e)); //frmErrorReport frm = new frmErrorReport(e); //frm.ShowDialog(); <----- I've also tried this but I get the same results } } -- There are 10 kinds of people. Those who understand binary and those who don't.

    C 1 Reply Last reply
    0
    • R Rob Tomson

      I'm trying to implement an error report in my program so that when it crashes it pops up a dialog that allows you to fill in some information and then send it along through email. The problem that I'm having is when I debug the program in VS it crashes gracefully, just like it's supposed to. But when I run it as a standalone .exe it just errors as if there is no try/catch statement. This is what I have for my Main function. If there are any suggestions please let me know. static void Main() { try { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.Run(new frmMDI()); } catch(System.Exception e) { System.Windows.Forms.Application.Run(new frmErrorReport(e)); //frmErrorReport frm = new frmErrorReport(e); //frm.ShowDialog(); <----- I've also tried this but I get the same results } } -- There are 10 kinds of people. Those who understand binary and those who don't.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Put a messagebox showing the exception message as the first line of the catch. Perhaps in release mode, your debug dialog is throwing it's own exception, causing this problem ? Christian Graus - Microsoft MVP - C++

      R 1 Reply Last reply
      0
      • C Christian Graus

        Put a messagebox showing the exception message as the first line of the catch. Perhaps in release mode, your debug dialog is throwing it's own exception, causing this problem ? Christian Graus - Microsoft MVP - C++

        R Offline
        R Offline
        Rob Tomson
        wrote on last edited by
        #3

        I've already tried that and it doesn't even hit the dialog. When I run it in 'release mode' it won't even hit the 'end of try' dialog. It just errors. This is what I had. static void Main() { try { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.Run(new frmMDI()); System.Windows.Forms.MessageBox.Show("end of try"); } catch(System.Exception e) { System.Windows.Forms.MessageBox.Show("start of catch"); System.Windows.Forms.Application.Run(new frmErrorReport(e)); System.Windows.Forms.MessageBox.Show("end of catch"); //frmErrorReport frm = new frmErrorReport(e); //frm.ShowDialog(); } } -- There are 10 kinds of people. Those who understand binary and those who don't.

        C 1 Reply Last reply
        0
        • R Rob Tomson

          I've already tried that and it doesn't even hit the dialog. When I run it in 'release mode' it won't even hit the 'end of try' dialog. It just errors. This is what I had. static void Main() { try { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.Run(new frmMDI()); System.Windows.Forms.MessageBox.Show("end of try"); } catch(System.Exception e) { System.Windows.Forms.MessageBox.Show("start of catch"); System.Windows.Forms.Application.Run(new frmErrorReport(e)); System.Windows.Forms.MessageBox.Show("end of catch"); //frmErrorReport frm = new frmErrorReport(e); //frm.ShowDialog(); } } -- There are 10 kinds of people. Those who understand binary and those who don't.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Fair enough, just checking assumptions. Does the error occur in both places ? i.e. if you deliberatly divide by zero, does it throw in debug/crash in release ? Christian Graus - Microsoft MVP - C++

          R 1 Reply Last reply
          0
          • C Christian Graus

            Fair enough, just checking assumptions. Does the error occur in both places ? i.e. if you deliberatly divide by zero, does it throw in debug/crash in release ? Christian Graus - Microsoft MVP - C++

            R Offline
            R Offline
            Rob Tomson
            wrote on last edited by
            #5

            I deliberatly try to set an int equal to a textbox string. And of course it throws 'Input string was not in a correct format.' When I do this in debug mode it catches the error, shows my dialog and everything is fine. But if I do the same proceedure in release mode I get the dafult .NET error dialog and it doesn't show my dialog. Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

            C 1 Reply Last reply
            0
            • R Rob Tomson

              I deliberatly try to set an int equal to a textbox string. And of course it throws 'Input string was not in a correct format.' When I do this in debug mode it catches the error, shows my dialog and everything is fine. But if I do the same proceedure in release mode I get the dafult .NET error dialog and it doesn't show my dialog. Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              That is really weird. If it were me, I'd reinstall the framework, b/c it just shouldn't act that way, I don't believe. I certainly put base level try/catches in a lot of my code, so I can handle exceptions gracefully, just like you're doing here, without this problem. Christian Graus - Microsoft MVP - C++

              R 1 Reply Last reply
              0
              • C Christian Graus

                That is really weird. If it were me, I'd reinstall the framework, b/c it just shouldn't act that way, I don't believe. I certainly put base level try/catches in a lot of my code, so I can handle exceptions gracefully, just like you're doing here, without this problem. Christian Graus - Microsoft MVP - C++

                R Offline
                R Offline
                Rob Tomson
                wrote on last edited by
                #7

                Okay, I tried running my program on a couple different computers and they all had the same result; it would fatal error instead of using my error report. Does it matter that I am using an MDI form with child forms? Maybe it's crapping out because a child form is erroring but then that doesn't explain why it works in debug mode and not in release. Any suggestions? Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

                M 1 Reply Last reply
                0
                • R Rob Tomson

                  Okay, I tried running my program on a couple different computers and they all had the same result; it would fatal error instead of using my error report. Does it matter that I am using an MDI form with child forms? Maybe it's crapping out because a child form is erroring but then that doesn't explain why it works in debug mode and not in release. Any suggestions? Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

                  M Offline
                  M Offline
                  mav northwind
                  wrote on last edited by
                  #8

                  Ah yes, that behaviour is really a nuisance. I've come across it some time ago and had a discussion with someone from M$ in some newsgroups, but he didn't tell why .NET behaves like this. Basically, it's the same effect when you get when you encapsulate a ShowDialog() in a try/catch. When you run the app from VS.NET, the exception is caught, when you start the application from outside you get the unhandled exception dialog.:sigh: To fix the problem you'll have to add a ThreadException handler to your app and then rethrow the exception:

                  static void Main()
                  {
                  try
                  {
                  Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                  Application.Run(new Form1());
                  }
                  catch (Exception ex)
                  {
                  MessageBox.Show("Exception caught in Main:\n"+ex.ToString());
                  }
                  }

                  private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
                  {
                  throw e.Exception;
                  }

                  Hope this helps, mav

                  R 1 Reply Last reply
                  0
                  • M mav northwind

                    Ah yes, that behaviour is really a nuisance. I've come across it some time ago and had a discussion with someone from M$ in some newsgroups, but he didn't tell why .NET behaves like this. Basically, it's the same effect when you get when you encapsulate a ShowDialog() in a try/catch. When you run the app from VS.NET, the exception is caught, when you start the application from outside you get the unhandled exception dialog.:sigh: To fix the problem you'll have to add a ThreadException handler to your app and then rethrow the exception:

                    static void Main()
                    {
                    try
                    {
                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    Application.Run(new Form1());
                    }
                    catch (Exception ex)
                    {
                    MessageBox.Show("Exception caught in Main:\n"+ex.ToString());
                    }
                    }

                    private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
                    {
                    throw e.Exception;
                    }

                    Hope this helps, mav

                    R Offline
                    R Offline
                    Rob Tomson
                    wrote on last edited by
                    #9

                    Thank You! That worked like a charm. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups