Skip to main content

Posts

Showing posts with the label FaultException

Why normal .NET exception handling doesn't work in WCF?

Errors and exceptions are part of our programming life and WCF is no different. So, when we get errors in WCF, we would like to propagate those errors to our WCF client so that they can accordingly take actions. In order to demonstrate this, let’s go through the code of a simple service: public int Add(int number1, int number2) { return number1 + number2; } public double Divide(int number1, int number2) { return number1 / number2; } Both the above methods will perform some calculation and return the result to the client. Let’s say, now for some reason someone sent 2 nd parameter of Divide method as 0.  What will happen? Definitely code will throw an error or say DivideByZero exception. Isn’t it? Know how to handle this error? Most of the developers will simply decorate Divide method with Try-catch block and throw the exception, similar to our normal .NET exception handling mechanism as shown in be