We are writing WCF services which are utilizing COM+ application, now we are unable to decide on exception management i.e. whether to use error codes or exception for business errors. Let me give you an example:
public Document GetDocument(int docId)
Now there can be following outputs when you call this method
- throws exception if database is down
Can be easily handled via exception management
- Returns null if document not found
- Returns null if document is not in active state (Business case)
Now the above cases, they return the same output but the reasons are different and we want to show the reason to the client, what can be the best practice
One of the options we were thinking of
public Document GetDocument(int docId, out enum returnErrorCode)
so enum can have values DocumentNotFound, DocumentNotInActiveState etc
First of all we don¡¯t like this approach and secondly caller will have to do lot of if checks after the method call which is weird.
The other option we are thinking to have two type of base exceptions derived from WCF FaultException, one to handle all unknown/technical exceptions and one to handle pure business errors with ErrorCode in it. The only benefit we'll be getting with this approach that WCF clients will be handling just these exceptions instead of writing various if checks. But still not sure it¡¯s correct to handle business errors via exceptions.
Please advice.
Regards
Faraz