Tuesday, February 12, 2008

One more Debug.Fail

I wrote about Debug.Fail usage earlier (Debug.Fail). One more usage - in the exception handling routine. You can't throw an exception there because this exception will go to the client and client will be confused. So I recommend not to throw an exception (if e.g. HandleException method obtains null as one of the mandatory arguments) but do something like this:

Debug.Assert (exc == null, "Exception passed to the ExceptionHanlder.HandleException can not be null");
 
or 

if (exc == null)
{
    Debug.Fail("Exception passed to the ExceptionHanlder.HandleException can not be null");
    throw new MeaningfulException();
}

This prevents system from sending wrong confusing exception. Instead of this we can for example send SoapException with the message that operation couldn't be completed.

No comments: