views:

553

answers:

6

Does anybody have best practices for exception handling ?

When searching the web I find a lot of best practices on a code level (don't catch general exceptions, don't rethrow new exceptions etc.) What I am looking for is best practises on a higher level, stuff like :

  • within an application catch exceptions on the ui level.
  • log as much detail as possible, show friendly error messages
  • in more SOA like apps distinguish between functional exceptions (You ask for a specific customer and expect to find one , but find none) and technical exceptions (database offline)
  • don't use exceptions for functional exceptions
  • distinguish between fatal and non-fatal exceptions
  • distinguish between exceptions that make a retry possible or make retrying totally useless
  • patterns for alerting the maintenance people

Any thoughts and help are greatly appreciated, thanks.

+2  A: 

I like also to distinguish between:

  • exception due to the caller of a function
  • exception due to internal error within a function.

That is for me a clear way to separate:

  • dynamic exception (that can occurs, but do not need to be explicity catched, liek an Illegal argument)
  • static exception (that must be explicitly dealt with, because of a defect from the internals of the application)
VonC
+2  A: 

.NET Specific but definitely has some worthwhile info.

http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx

Eoin Campbell
+1  A: 

A good article from Joel about exceptions (IMHO).

Ilya
Why is this article "good"? As far as I can tell, Joel is basically saying "Wow, using exceptions sucks, but so do the alternatives - life sucks." Not exactly a shining beacon of enlightment:)
Scotty Allen
I added IMHO to the post. Clearly i think it's good and i think that there is an explanation why exceptions sucks, you might disagree, but you better argue his argument than just disagree. Hidden semantics are not so good and i'm totally agree with him on that.
Ilya
take a look on Rasmus Faber links in post above.
Ilya
A: 

There is an article in Msdn library.

yapiskan
A: 

You could do a lot worse than look through the code and documentation for Microsoft's Exception Management Application Block. It's probably overkill for a lot of scenarios, but it's certainly comprehensive.

Stu Mackellar
It took me some time to look at this and it has some interesting things in it. But it is still to low-level and a bit old. Thanks for the effort
KeesDijk
Another look reveals that it's been updated more recently (October 2008) as part of the wider-ranging Enterprise Library.http://msdn.microsoft.com/en-gb/library/cc467894.aspx – Stu
Stu Mackellar
+5  A: 

@Ilya:

That is probably one of the worst article Joel has ever written (for those who haven't read the link, he is arguing "Exceptions considered harmful", so do not use them).

Joel has two problems with exceptions:

  1. They are invisible in the source code.

    • But so are unhandled status-returns. And properly handled status-returns clutter up the normal flow of the methods making them much harder to read.
  2. They create too many possible exit points for a function.

    • And so what? Handling a failure will almost always require you to return early. Making the exit points explicit only serve to clutter up the code.

Ned Batchelder has an excellent (and much longer) reply to Joel here. Joel has a short reply here, to which Ned replies again here.

Brad Abrams also has a very nice article on the value of exceptions here.

Rasmus Faber
From this link is a link to http://nedbatchelder.com/text/exceptions-in-the-rainforest.html, which is a nice beginning for what I am looking for. Thanks
KeesDijk
I'm a C person and don't like the idea to work without goto as well. So I'm not completely agree with Joel, but he definitely has a point. And article is relevant at least for the reason that it's give you another point of view and for me possibility to read reply article as you mentioned.
Ilya
Actually after reading the replay i was not convinced. But i don't want to start theoretical discussion here.
Ilya