exception not catching
-
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 myMain
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. -
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 myMain
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.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++
-
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++
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. -
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.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++
-
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++
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.
-
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.
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++
-
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++
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.
-
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.
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
-
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
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.