在软件服务中使用包含错误代码的异常或字符串
我们正在为客户的网站创建一个应用程序。该网站将对我们的应用程序进行函数调用以生成 XML 数据,我们将其作为字符串返回。如果我们在处理过程中出现问题,我们应该如何报告这个错误?我们应该抛出一个异常让客户端的网站捕获,还是应该返回一个包含错误代码(而不是 XML 数据)的字符串?哪个有意义和/或者是更好的做法?
预先感谢您对我们的帮助!
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 of the XML data)? Which makes sense and/or is the better practice?
Thank you in advance for helping us out!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为有两种方法可以做到这一点。
I think there are two ways you can do that.
我倾向于说错误代码几乎总是一个坏主意。异常提供了一个合理的默认错误处理方案:如果您不处理它,您就有效地断言它不会在您的情况下发生。如果你错了,你的程序会很快失败,并带有清晰的错误消息。返回代码不提供此类保证。这里的默认行为是错误被隐藏起来,程序继续运行,就像什么都没发生一样。
此规则的例外情况:
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: