开发者

Use of Exceptions or Strings containing error codes in Software Services

开发者 https://www.devze.com 2022-12-19 03:43 出处:网络
We are creating an application for a client\'s website.The website will make a function call to our application to generate XML data, which we will return as a String.If something goes wrong during th

We are creating an application for a client's website. The website will make a function call to our application to generate XML data, which we will return as a String. If something goes wrong during the course of our processing, how should we report this error? Should we throw an Exception for the client's website to catch, or should we return a String containing the error code (instead o开发者_如何学Pythonf the XML data)? Which makes sense and/or is the better practice?

Thank you in advance for helping us out!


I think there are two ways you can do that.

  1. Include an Error-String Element into the Response-Element of every Web-Service Function. If it is empty, then the function call was succesful. If it is set, then there was an exception and the Error-String tells you the reason.
  2. Use the SOAP-Fault Element for exception handling. I think every SOAP-Toolkit offers a way to fill this Element (for Example look at gSoap Framework http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc11)


I'm inclined to say that error codes are almost always a bad idea. Exceptions provide a sane default error handling scheme: If you don't handle it, you effectively assert that it can't happen in your situation. If you're wrong, your program fails fast and with a clear error message. Return codes offer no such guarantees. The default behavior here is that the error gets swept under the rug and the program continues as if nothing happened.

Exceptions to this rule:

  1. If the error is very minor, such that continuing as if nothing happened is a better default than crashing.
  2. In very resource-constrained environments (i.e. not servers), exceptions can be expensive.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号