简体   繁体   中英

How to handle Unhandled Exceptions in .NET COmpact Framework 3.5?

In .NET CF, is there any event like Application.DispatcherUnhandledException that allows us to capture all the un-handled exceptions to log as well as to keep the application open with out crashing?

The arguments of DispatcherUnhandledException event handlers allows to stop app crashing by just setting

// Prevent default unhandled exception processing

e.Handled = true;

The event AppDomain.CurrentDomain.UnhandledException allows to capture the unhandled exceptions, but seems to be there is no way to stop the application from closing.

I need similar functionality provided by DispatcherUnhandledException in .NET CF. Please help me.

Nope. You have an opportunity to LOG any unhandled exceptions via the AppDomain.CurrentDomain.UnhandledException event. However this will not catch the exception and allow you to continue.

To be frank, your desire to "keep the application open with out crashing" is an understandable yet naive aim. I have seen lots of software that attempts to do this and most of it is utter crap. If something in your application has gone wrong and you failed to have an explicit catch for then you don't know what has gone wrong and continuing is dangerous at best.

Instead you should endeavour to understand where exceptions may occur in your program and write appropriate handlers for those routines at a later date . You can log the exceptions as you've already noticed and get those reports back from the field. Then you can write appropriate handlers to handle those errors.

With my last project we did implement a "catch-all" (for the UI thread) but this would just halt the application, inform the user of the problem and close the app (another service would then restart it). This was actually quite easy as we re-route all keypresses and clicks through one point (to enable easy configuration of keys and screens). It might be more tricky to do if you are just using standard forms.

Also ensure you also write high level catches on background threads as an unhandled exception on a background thread will kill off your app instantly. In this case we typically marshalled the thread back to the UI "catch-all" handler (via an interface) that (again) displayed a message, logged the exception and closed the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM