-1

I have created some custom exceptions in a WPF project and I am pretty sure that I am not catching them, when they get thrown, but my application does not crash either. The Exception is logged in the Output Window though.

My custom exceptions derive from System.Exception and I have checked all my try{}catch{} statements if they catch my custom Exception or System.Exception. None of them do that. I am using Visual Studio 2010 and am running it on a 32bit machine, so this seems to be a different problem.

Community
  • 1
  • 1
FlyingFoX
  • 3,379
  • 3
  • 32
  • 49
  • 3
    How do you know they are not being caught, Have you tried checking the box to [break on thrown](http://msdn.microsoft.com/en-us/library/vstudio/d14azbfh.aspx) exceptions so your code will break even if the exception is in side a try/catch block? Also is the code causing the exception running on another thread (like in a task or a background worker), some of those things have special ways for checking exceptions. – Scott Chamberlain Aug 15 '13 at 15:06
  • 1
    Are you throwing them on a background thread? – Dan Puzey Aug 15 '13 at 15:07
  • If background threads throw unhandled exceptions, I _think_ they may just fail silently. Try hooking into some of the application unhandled exception handlers (http://stackoverflow.com/questions/1472498/wpf-global-exception-handler). Also, if your output window is reporting something like "first chance exception" it may be from your debugger informing you, but these exceptions may be caught and handled anyway (which may point to smells of using exceptions as flow control). (http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx) – Chris Sinclair Aug 15 '13 at 15:07
  • 2
    you need to show the code – Ehsan Aug 15 '13 at 15:08
  • @DanPuzey I have enabled the break on thrown exception and VS says the exception is thrown on MainThread. – FlyingFoX Aug 15 '13 at 15:29
  • 1
    @ChrisSinclair that was the behavior of .NET 1.x. Unhandled exceptions terminate the process in .NET 2.0 and onwards. – Brian Rasmussen Aug 15 '13 at 15:46

1 Answers1

0

You can add a handler for the AppDomain.UnhandledException event:

AppDomain.CurrentDomain.UnhandledException += App_UnhandledException;

public void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception exception = (Exception)e.ExceptionObject;
    Error error = new Error(exception, mainViewModel.StateManager.CurrentUser);        
    mainViewModel.AddError(error); 
}

Error is a custom class that I use to log errors in the database with. If you put a break point in this method, then execution will jump here whenever there are any uncaught Exception objects that have been thrown.

Sheridan
  • 68,826
  • 24
  • 143
  • 183