I need a point of view in this design that I am proposing for handling exceptions.
I have a wcf service that throws different types of fault exceptions:
- throw new FaultException < BusinessRuleViolationFault >(...)
- throw new FaultException < SomeOtherViolationFault >(...)
- ...
- ...
All these fault exception classes implement an interface called IViolationFault, which has a single HandleException method. The implementation of each such method is different depending on the class they are in; so for example when these exception classes are caught in the client, all I need to do is just call that HandleException() method and it will be taken care of without me having to write an IF condition to differentiate between the types of errors. In BusinessRuleViolationFault's HandleException() method I might just want to show a message on the screen, while in the other one I might want to log it somewhere and trigger some other action as well...
catch (FaultException<BusinessRuleViolationFault> ex)
{
ex.HandleException();
}
catch (FaultException<SomeOtherViolationFault> ex)
{
ex.HandleException();
}
Questions
- Is there any thing wrong in this approach?
- Is there a way I can reduce the number of catch blocks and have just one catch block take care of all exception types?
- Is this a good object oriented way to acheive polymorphism while handling exceptions?
EDIT
Ive changed the code to inherit from a base class instead of implementing an interface. My BusinessRuleViolationFault class has the HandleException method, but I am not able to get the HandleException method in the client catch block. What am doing wrong? This is how it is thrown in the service
BusinessRuleViolationFault bf = new BusinessRuleViolationFault("");
throw new FaultException<BusinessRuleViolationFault>(bf, new FaultReason(new FaultReasonText("Fault reason here")));
This is my BusinessRuleViolationFault code
[DataContract]
public class BusinessRuleViolationFault : BaseFault
{
public BusinessRuleViolationFault(string message)
: base(message)
{
}
[OperationContract]
public override string HandleException()
{
return "BusinessRuleViolationFault executed";
}
}
[DataContract]
public abstract class BaseFault
{
public BaseFault(string message)
{
Message = message;
}
[DataMember]
public string Message { get; set; }
[OperationContract]
public abstract string HandleException();
}
Do let me have your thoughts on this. Thanks for your time...