4

In MainFile:

Window window = new MyDialogWindow();
try
{
    window.ShowDialog();
}
catch
{
    // This is never called !
}

In MyDialogWindow:

private void MyDialogWindow_Closing( object sender, CancelEventArgs e )
{
    throw new Exception();
}

It seems that the exception thrown into the closing event handler never reach the catch block of the caller. Do you know why ?

EDIT: I should mention I've checked that MyDialogWindow_Closing is called after I press on the close button.

EDIT2: When listening to AppDomain.CurrentDomain.UnhandledException, the event is fired !

jchristin
  • 7,352
  • 2
  • 15
  • 18
  • Does your dialog *actually* close? Or does throwing the exception cause the close to be cancelled? – Trevor Elliott Oct 04 '13 at 20:24
  • The dialog is still there, with its content entirely black. And if I add "window.Show();" just below window.ShowDialog();" I get this exception: "System.InvalidOperationException: Cannot set Visibility to Visible or call Show, ShowDialog, Close, or WindowInteropHelper.EnsureHandle while a Window is closing". So yes, it seems the exception prevent the dialog form closing. – jchristin Oct 04 '13 at 20:34
  • In my sample app with your code (and subscribing to `window.Closing`) catch is called. – LPL Oct 04 '13 at 20:39
  • tried it... catch does gets the call – Nitin Oct 04 '13 at 20:44
  • Could you guys send me your sample app at jul*dot*christin*at*gmail*dot*com ? Doesn't work on my side even by creating a new app. – jchristin Oct 04 '13 at 20:59
  • For what it's worth. I get what @JulienChristin is describing. `Catch` is never hit, and the `Window` remains open with black interior. – Khan Oct 04 '13 at 21:28
  • Julien, did you tried `MyDialogWindow window = new MyDialogWindow();`? Anyway, here's mine working: [ShowDIalog](http://www.sendspace.com/file/likwaz) – trinaldi Oct 05 '13 at 00:09
  • I had asked if it was actually closing because in my case it does not close if I throw an exception in the Closing event. Since the Closing event is cancellable (it passes a CancelEventArgs) then it could simply be catching the exception and cancelling the close. I would recommend simply not throwing an unhandled exception in a Closing event. – Trevor Elliott Oct 05 '13 at 18:47
  • @Tico Thank you for the link. I compiled it but doesn't work: the exception is not caught by the try/catch block in MainWindow.xaml.cs. But if I listen to AppDomain.CurrentDomain.UnhandledException, the event is fired. Still a Mistery ! – jchristin Oct 07 '13 at 13:29
  • Turn on all the exceptions. I bet his exception is not even getting a chance to be thrown. Debug-->Exceptions. Click all. Run again. – ouflak Oct 17 '13 at 13:24

1 Answers1

0

I would guess that you are getting an exception that is not your thrown exception, but instead another exception that is not normally 'caught' by the .Net exception handling umbrella. One such class of exceptions are structured error handling exceptions (SEH) which typically occur when there are issues (usually memory issues, or pointer issues) in unmanaged code that crash in that execution, but can't be bubbled up into the .Net exception handling stream.

Go into your Debug-->Exceptions, and check everything. Then run your program. You will almost certainly find out what your exception truly is.

It can't hurt to look at this question as well which discusses the topic.

Community
  • 1
  • 1
ouflak
  • 2,458
  • 10
  • 44
  • 49