I have a WPF application written for the .NET 4 Full Framework. The application uses SQL Anywhere as its database. My application has an unhandled exception handler, which always logs errors to a custom event log for the program. It then displays the error message to the user. The program also sends messages to the event log whenever it's about to do something in order too make debugging it easier.
The application is installed on a user's laptop, which is running Windows 7 and has 8 GB of RAM. When it is started on this machine, the splash screen is displayed and then the program's main window is displayed. Less than a second after being drawn, the program dies. There are no error messages displayed.
Checking the event log shows that the program's last message written was that it was doing a check for the existence of a user in the database. There are no error messages.
The code that follows the last message that was displayed is a call to a method that does some parameter checking and then executes the following EF query:
LPRCore.CarSystem.User user = null;
IQueryable<User> query = from u in context.Users
from m in context.Members.Where( m => m.UserId == u.UserId )
.DefaultIfEmpty()
where u.LoweredUserName == userName.ToLower() && m == null
select u;
try {
user = query.SingleOrDefault();
} catch ( Exception ex ) {
....
}
I can't tell if the code in the catch block is ever called. My suspicion is that it is getting called and an exception is occurring in there.
My question is, if an exception occurs in a catch block, won't that exception be caught by the Unhandled Exception handler at the upper level, if there is no other exception handler to catch the error? Or would it cause the program to die without reporting anything?
My question is, if an exception occurs in a catch block, won't that exception be caught by the Unhandled Exception handler at the upper level, if there is no other exception handler to catch the error? Or would it cause the program to die without reporting anything?
The exception will propagate upwards, and should be caught by the unhandled exception handlers.
That being said, some exceptions won't be caught, such as StackOverflowException. It's also possible that the code in the exception block (or DB provider...?) is terminating the process (ie: calling Environment.Exit or something just as bad) in a way that won't allow the exception handling to work.
In short an unhandled exception
Remember there are at least 2 places you should log unhandled exceptions. The Application.DispatcherUnhandledException and EACH of the application's AppDomain UnhandledException handlers.
If you don't catch these the application will terminate.
Also be aware that since .NET 2.0 an unhandled exception in a thread propagates to the application, resulting in it terminating (the app that is not the thread). In .NET 1.1 the thread just quietly died.
Starting with the .NET Framework version 2.0, the common language runtime allows most unhandled exceptions in threads to proceed naturally. In most cases this means that the unhandled exception causes the application to terminate.
(Exceptions in Managed Threads)
The Application object's ThreadException handler only seems to catch exceptions that propagate up from the applications main thread (or probably more correctly exceptions raised in the applications dispatcher thread). Everything else seems to come up on the application domain's UnhandledException handler.
So in addition to an Application.UnhandledException handler add domain.UnhandledException like this:
static void Main(string[] args)
{
Application.ThreadException += ApplicationThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// Log exception here and exit application, you can't recover now.
}
As a point to note, if you ever end up in any of these "global" exception handers, your app is in an unknown but broken state, and the only valid thing to do is log the issue and quit/restart the application; you cannot recover at this point, even if you can still let the application run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With