views:

348

answers:

5

I am trying to find out the best way of handling exceptions, I have a number of layers to my application and started to use a return type of BOOL i.e. if it fails then return False and if it succeeds return True..

This works great in methods like SaveMyRecord(somerecord); as i am passing in values and don't require anything returned so i can use the return type of bool to indicate if it succeeds or not.

But then it got me thinking that things like GetMyRecord() actually returns type of IQueryable hence i can't use a bool to tell me if it failed or not.

The thing is i am handle alot of my errors where they happen with try and catch and hence don't want the client to receive an exception.

Maybe there is a better way, i then got thinking about using OUT parameters BUT this means i need to change the signature of all methods and add aditional params..

Maybe i should be passing the exception back to the CLIENT and handling it there?

I am a little lost,

Is there some standards or any docs to adivse best practices?

+5  A: 

You should start reading Design Guidelines for Exceptions

Then, depending on your scenario, you should take other considerations into account such as Exception Shielding.

For instance: If you are using web services (ASMX or WCF) as a back-end, you may want to take a look at Improving Web Services Security and read the parts concerning exception handling.

Alfred Myers
+6  A: 

Bubble up the exception to the CLIENT and handle it there. Definitely pass it in full detail all the way up. Most best practices almost entirely agree on this, always finally handle on the perimeter, in this case the CLIENT, though in other cases that could be a web service.

Only catch if you want to log it, add more info to it or try and recover from a particular exception. In each case you will will either throw a new exception with the original as the inner or simply 'throw' the original as is, and as pointed in comments don't 'throw ex'

This question is a near duplicate and you'd find lots of existing very well answered questions on SO. I answered a similar one only yesterday

dove
As far as catching only to log, this is OK as long as you also rethrow the exception after it has been logged. If you do that make sure to just use the `throw` statement instead of `throw ex` so you don't break the stack trace.
Scott Dorman
@Scott absolutely. also if you're adding more info then put original exception as an inner exception to the new one you create. and if you can't recover after n times then throw as you say. thinking you know all this ;) but thought it might help other readers
dove
Agreed with Scott and from personal experience: if you try to build a more than moderately complex application where your error conditions are based on boolean return values you will very soon see spaghetti coding and realize how little you know about what went wrong when a false is returned from the depths of a subroutine written months ago. The basic problem is that return values have to be threaded up to code that can handle it via many layers whereas exceptions "bubble up" to where they can be handled by design.
Paul Sasik
THanks everyone for some great advice!! - I am going to apply it now!!.. Basically i am going ot use try - catch and Log and throw; to rethrow the same error so that it bubbles up to the client where i will handle it..
mark smith
A: 

This a great question!

Don't code for exception. For the most part pretend they never happened. I worry about exception in two places: Displaying error feedback to the user and resource management (i.e. closing an open file when a exception is throw).

Chuck Conway
+1  A: 

If a method can't do it's job, it should throw an exception. Never return an exception as a result.

bryanbcook
+1  A: 

The approach that is recommended and considered a best practice is to use exceptions. You can (and should) read the Framework Design Guidelines (2nd Ed.), which has guidelines for exceptions and the try-parse pattern.

There are a few problems with using return codes (either numeric or boolean), the two biggest being:

  • Easily overlooked/ignored by programmers.
  • Can't be used in all situations. What happens if your constructor fails? It's not possible for you to return a value explicitly from a constructor.

As for when to handle exceptions, you should only handle them when you can do something meaningful about the exception. The problem with always handling exceptions so the client never sees them is that you can end up handling an exception that you shouldn't have and cause more problems later (like actually loosing data).

Scott Dorman