public int Divide(MyNumbers obj) { int result = 0; try { if (obj.Number2 > 100) { throw new FaultException("Value of B cannot be above 100", new FaultCode("BValueIsAboveHundred")); } result = obj.Number1 / obj.Number2; } //catch(System.DivideByZeroException dbze) //{ // throw new DivideByZeroException(dbze.Message); //} //catch (Exception ex) //{ // //throw new Exception(ex.Message); // //throw new FaultException("Vaue of B cannot be zero."); // if (!ex.Message.Equals("Value of B cannot be above 100")) // throw new FaultException("Value of B cannot be zero", new FaultCode("BValueIsZero")); // else // throw new FaultException("Value of B cannot be above 100", new FaultCode("BValueIsAboveHundred")); //} catch (Exception ex) { var divisionFault = new DivisionFault(); divisionFault.Method = "Divide"; divisionFault.Reason = "Value of B cannot be zero"; divisionFault.Message = ex.Message; throw new FaultException <DivisionFault>(divisionFault); } return(result); }
public int Divide(MyNumbers Obj) { int result = 0; // In WCF .Net Exceptions are not recommended // Best practise is Fault exceptions (Fault contracts) try { if (Obj.Number2 > 100) { throw new Exception("Wrong Value"); } result = Obj.Number1 / Obj.Number2; } //catch (System.DivideByZeroException dex) //{ // throw new DivideByZeroException(dex.Message); //} //catch (Exception ex) //{ // //throw new Exception(ex.Message); // if (ex.Message.StartsWith("Wrong")) // throw new FaultException("Value of divisor cannot be above 100!!!!", new FaultCode("DivisorAbove100")); // else // throw new FaultException("Division by zero!!!!", new FaultCode("DivisorIsZero")); //} catch (Exception ex) { var df = new DivisionFault { Method = "Divide", Reason = "Divisor value cannot be zero", Message = ex.Message }; throw new FaultException <DivisionFault>(df); } return(result); }