1

So, I have this situation:

Solution

  • -Project A
    -FrmA
  • -Project B
    -FrmB

My main app is in Project A, within this FrmA form there's a method that instatiates and call a method on FrmB within a try..catch which is inside a Project B, this FrmB has an throw within a second try..catch block which catchs the exception fine but doesn't throw it back to frmA catch block. Something like this:

FrmA:

try{
 frmB.show();
}catch(Exception ex){
  //Do things
}

FrmB_onLoad():

try{
 object.method(); //method which generates the Exception 
}catch(Exception ex){
  throw ex; //which should go to frmA method
}

Now, the most intriguing thing is that when it's on debug mode or release mode on the Visual Studio it works great, but when I publish it to production I get this strange not-catching on try..catch on frmA.

Any guesses?

PS: I'm using VS2005(yeah, i know..)

Markissimo
  • 309
  • 2
  • 9
  • 23
  • 3
    FYI - to rethrow an exception, you can simply type `throw;` in your `catch` block. – Forest Kunecke Aug 02 '13 at 20:09
  • 3
    I think we need to see more code. For example, you may have coded it so that the exception is really only thrown during debugging, and if so we can't help you with just what you've shown here. – Geeky Guy Aug 02 '13 at 20:10
  • 5
    @FKunecke - `throw` and `throw ex` are two very different things. `throw` maintains the original stacktrace whereas `throw ex` resets the stacktrace from that line. Can be a very important difference. – Haney Aug 02 '13 at 20:17
  • 1
    Running on 64bit system as AnyCPU? – Steve Aug 02 '13 at 20:17
  • Can you post the contents of your `object.method()`? That would help the most. – Forest Kunecke Aug 02 '13 at 20:23

3 Answers3

3

Hans Passant explains this behavior very well in response to another question on this site.

In short, WinForms creates a message pump when a modal dialog is up, so the framework catches all exceptions that bubble up to the message pump. When running under debugger, the framework instead allows exceptions to pass out of the modal dialog to the calling code, presumably so that the developer notices when exceptions are uncaught.

If setting a global unhandled exception handler is not desirable (and it has not been desirable in the applications I have worked on) you could instead catch exceptions thrown in your Load event handler, for instance, and assign the caught exception to a new public property on your Form. Clients of that dialog would then need to check if that exception property was set after ShowDialog() returns. This is not a great solution - it is hard to catch ALL exceptions coming out of the form, and it also makes it easy for clients to forget to check that property when the dialog is dismissed - but it can work in some circumstances.

Community
  • 1
  • 1
Matt Dillard
  • 14,677
  • 7
  • 51
  • 61
  • Setting the UnhandledExceptionMode didn't work either, but I'm accepting this as answer for explaining why this is happening, I think I'll handle this exception on form B and try to make this work. Thank you – Markissimo Aug 05 '13 at 12:01
1

Try this (solved a similar problem for me):

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += (o, e) => MessageBox.Show(e);
AppDomain.CurrentDomain.UnhandledException += (o, e) => MessageBox.Show(e);
Mark Toman
  • 3,090
  • 2
  • 17
  • 18
0

Run it outside VS on your dev machine. Does the exception get caught?
If it does; you might be running the wrong DLL in production - something happens when you deploy.
Then
Copy the correct DLLs manually to production and try again.

HTH

LosManos
  • 7,195
  • 6
  • 56
  • 107