views:

281

answers:

3

We have exception catching code in most of our event handlers etc, this leads to very complex logic, with flags that are set to say if there has been an exception so as not do the next step etc.

At first sight I would move all exception report/logging to the AppDomain.UnhandledException event, however from my experience with WinForms this will lead to a lot of exceptions being lost.

Also when there is an exception we have include details of the operation the user was trying to do in the log message.

So what are people experiences both bad and good at exception logging/reporting/recovering in WCF applications?

(I would love to say that we had something like the Model-View ViewModel (MVVM) pattern) in use, but we don’t and are a long way from being able to use any “clean” design like that)

+1  A: 

Its not specific to WPF, but the best place to handle exceptions is to handle them at the point where user interaction with the form is converted into a logic process. This is either in the codebehind or in a controller method.

Only at this level do you know what the user is trying to do and what reasonable steps to take when an exceptional situation is encountered.

Of course, if you don't know what exceptions may be thrown don't try to handle them. And don't bother handling exceptions that you can't do anything about.

Will
I am trying to fight the "hide the problem and keep going, then the customer will not complain about a crush" problem. Given that the customer would like to be able to use part of the system even when all of it is not working, this is not a unreasonable requirement.
Ian Ringrose
@ian Using "part of the even when all of it is not working" is a good way to end up with corruption in the system. If you can handle an exception, awesome. Handle it. If you don't know, its better to fail fast than end up in an unknown state. What your customer wants isn't necessarily what they actually need. The hardest part of being a developer is sometimes telling your customers "no."
Will
A: 

Maybe the answers to the "Where do you like to catch exceptions and why?" question are helpful here as well?

peSHIr
A: 

You should never have to use flags to say exceptions have been handled - that smells like bad design.

Exceptions fall into two categories:

  • expected (e.g. validation failed, data could not be put into database)
  • unexpected

your expected ones should be handled pretty quickly, and logged depending on the type of exception. For instance, if the user entered some data that was rejected by validation code in the business layer, i would catch the exception and notify the user, but not log it - because it was expected and i can deal with it. Others could be "expected", but you cannot deal with it - like a WCF call failed due to timeout or oversized data packet. This you should definately log - you may even be able to recover from it, so once again it should be caught and dealt with. Note the lack of flags - an exception is either dealt with, or it continues to bubble up. If you need to take an action you can do so, and then rethrow the exception to let it bubble up further - look, still no flags :)

Another approach i have taken in the past when throwing (custom) expected exceptions in an ASP.NET application is to mark it as being capable of being handled locally or not. This means that when the aspx caught the error (generic error handler in a base page that all apsx's inherited from), then it knew whether it should just show it locally within the page (after doing a text lookup in a resource file), or whether it should redirect to an error page. This approach was especially useful when doing a mixture of standard postbacks and ajax callbacks (may not be particularly useful to WPF apps though).

For major unexpected errors, you can catch those at the Application level, here is a previous SO post about it. Another two related posts that might be useful here, and here.

Another thing i should mention is to make sure your error logging is relatively bulletproof - there is nothing worse than your exception logging process throwing an exception, and losing all the valuable details of that tricky bug you are trying to track down for that irate user.

slugster