6

I have created a new WPF application and added an event handler for Loaded event in MainWindow:

Loaded += (s, e) => { throw new Exception("AAAA!"); };

Then i start this application from Visual C# and the application doesn't crash nor show an uncaught exception.

I expect that it would crash and this application indeed crashes on other computers. But why does it work on mine?

Update Added a screenshot:screenshot

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
meze
  • 14,975
  • 4
  • 47
  • 52

2 Answers2

6

To catch the exception you need to either do a try/catch in the Loaded method, or you can tell the Dispatcher to notify you when an unhandled exception occurs.

Try the following, for instance in the OnStartup method of your application:

App.Current.DispatcherUnhandledException += (s,e) => 
{ 
  // Handle the exception here
  var ex = e.Exception; 
};

Edit:

If you want the application to crash then try the following:

App.Current.DispatcherUnhandledException += (s,e) => 
{ 
  // this should cause your application to crash :-)
  throw new Exception("Now it should crash!", e.Exception);
};

The difference here is that we create a new exception that is thrown on the UI-thread.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
  • Yes, I know. But why don't I see the standard Windows dialog "Application has stopped working" when i don't have `DispatcherUnhandledException`? – meze May 09 '11 at 10:58
  • If you expect your app to crash then the exception must be raised on the ui thread. Try to raise a new exception inside the exception handler. – Rune Grimstad May 09 '11 at 12:23
  • @meze: Updated my answer with a version that crashes :-) – Rune Grimstad May 09 '11 at 12:25
  • 1
    @Rune Grimstad, updated my code and the application doesn't crash. The window stays displayed like nothing has happen. Added `System.IO.File.WriteAllText("c:/ex", "caught")` and `c:/ex` was created. So `DispatcherUnhandledException` was called. – meze May 09 '11 at 12:45
  • @meze: Interesting. Did you throw a new exception inside DispatcherUnhandledException? – Rune Grimstad May 09 '11 at 13:07
  • 1
    @Rune Grimstad: yes. If i throw it outside `DispatcherUnhandledException`, the application will crash. But won't if it's inside. – meze May 09 '11 at 13:09
  • @meze: Interesting and surprising. It would be interesting to see your source code. – Rune Grimstad May 09 '11 at 13:12
  • @Rune Grimstad: I decided to attach a screenshot with the code to my question ;) – meze May 09 '11 at 14:11
4

The Loaded event is propably called from a background thread. When an exception is thrown in this thread, it will be terminated, but will not affect your main application thread. This behaviour can be seen in many event handlers, e.g. a Timer_Elapsed event handler will also not affect your code generally. This dows not mean you shouldn't care about the exceptions inside such code!

eFloh
  • 2,098
  • 20
  • 24
  • How can i check that? I added `Thread.Sleep(1000)` before throwing an exception and the whole window hangs for 1 sec. So i expect they are in the same thread. – meze May 09 '11 at 11:08
  • you could try to copare Thread.Current with this.Dispatcher.Thread to detect this and/or check the result of this.Dispatcher.CheckAccess(). But maybe you don't have to, what do you want to achieve? (FU'2 the answers ans comments above mine, those seem to be on the same trail...) – eFloh May 10 '11 at 10:13
  • 2
    As a developer, I want my application to crash if i haven't caught all exceptions. I don't want my application to keep running when something is broken. I also don't get why `Loaded` would be fired on another thread. I just tried this: `Loaded += (s, e) => { if (this.Dispatcher.Thread.Equals(Thread.CurrentThread)) Thread.Sleep(4000); throw new Exception("AAAA!"); };`. The window freezes for 4 seconds and then everything is fine, no "this application has stopped working" dialog. – meze May 10 '11 at 15:31
  • 1
    wow, the exception being consumed even on the UI thread... does this only happen when run from within VS or even when started directly? I suppopse it hase something to do with the bittness of you application, see [link](http://stackoverflow.com/questions/4807122/wpf-showdialog-swallowing-exceptions-during-window-load) for hints... – eFloh May 13 '11 at 10:31
  • thanks for the link. The solution in the answer works in 90% cases, but seems because 64bit machines unable to properly throw exceptions.. – meze May 13 '11 at 15:19