<p>My group is developing a service-based (.NET WCF) application and we're trying to decide how to handle exceptions in our internal services.  Should we throw exceptions?  Return exceptions serialized as XML?  Just return an error code?</p>

<p>Keep in mind that the user will never see these exceptions, it's only for other parts of the application.</p>

[{'answer_id': 40140, 'author': 'Phil Bennett', 'author_id': 2995, 'author_profile': 'https://Stackoverflow.com/users/2995', 'pm_score': 1, 'selected': False, 'text': "<p>I'm a bit confused, I'm not being flippant -- you say you want to return exceptions serialised as XML on the one hand and that the user will never see the exceptions on the other hand.  Who will be seeing these exceptions?</p>\n\n<p>Normally I'd say to use WCF fault contracts.  </p>\n"}, {'answer_id': 40153, 'author': 'hova', 'author_id': 2170, 'author_profile': 'https://Stackoverflow.com/users/2170', 'pm_score': 2, 'selected': False, 'text': '<p>Well, why not just throw the standard SOAPExceptions?   The problem with error codes and serialized XML is that they both require additional logic to recognize that an error did in fact happen.  Such an approach is only useful if you have specialized logging or logic that needs to happen on the other side of the web service.  Such an example would be returning a flag that says "it\'s ok to continue" with an error exception report.  </p>\n\n<p>Regardless of how you throw it, it won\'t make the job any easier, as the calling side still needs to recognize there was an exception and deal with it.  </p>\n'}, {'answer_id': 40164, 'author': 'brien', 'author_id': 4219, 'author_profile': 'https://Stackoverflow.com/users/4219', 'pm_score': 0, 'selected': False, 'text': '<p>Phil, different parts of the application call each other using WCF.  By "return exceptions serialized as XML," I meant that the return value of the function wold be an exception object.  Success would be indicated by null.  </p>\n\n<p>I don\'t think that\'s the right option.</p>\n\n<p>WCF fault contracts sound good, but I don\'t know anything about them.  Checking google right now.</p>\n'}, {'answer_id': 40170, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': True, 'text': '<p>WCF uses <code>SoapFaults</code> as its native way of transmitting exceptions from either the service to the client, or the client to the service.</p>\n\n<p>You can declare a custom SOAP fault using the <code>FaultContract</code> attribute in your contract interface:</p>\n\n<p>For example:</p>\n\n<pre><code>[ServiceContract(Namespace="foobar")]\ninterface IContract\n{\n    [OperationContract]\n    [FaultContract(typeof(CustomFault))]\n    void DoSomething();\n}\n\n\n[DataContract(Namespace="Foobar")]\nclass CustomFault\n{\n    [DataMember]\n    public string error;\n\n    public CustomFault(string err)\n    {\n        error = err;\n    }\n}\n\nclass myService : IContract\n{\n    public void DoSomething()\n    {\n        throw new FaultException&lt;CustomFault&gt;( new CustomFault("Custom Exception!"));\n    }\n}\n</code></pre>\n'}, {'answer_id': 65072, 'author': 'wojo', 'author_id': 9022, 'author_profile': 'https://Stackoverflow.com/users/9022', 'pm_score': 0, 'selected': False, 'text': '<p>I would avoid sending exceptions directly back to the client unless you are okay with that much detail being sent back.</p>\n\n<p>I would recommend using WCF faults to transmit your error message and code (something that can be used to make a decision on the receiver to retry, error out, etc) depending if it is the sender or receiver at fault.</p>\n\n<p>This can be done using FaultCode.CreateReceiverFaultCode and FaultCode.CreateSenderFaultCode.</p>\n\n<p>I\'m in the process of going through this right now, but ran into a nasty snag it seems in the WCF fault generated SOAP 1.1 response. If you are interested, you can check out my question about it here:</p>\n\n<p><a href="https://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values">.NET WCF faults generating incorrect SOAP 1.1 faultcode values</a></p>\n'}]
['https://Stackoverflow.com/questions/40122', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4219/']