且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理API中的错误?

更新时间:2023-01-12 14:52:08

p>我会使用方法编号1.这是苹果通常在他们的API,如保存文件等等。这种方法的好处是能够快速查看它是通过还是失败(通过返回一个布尔),并立即可以访问该错误。这大体上与数字2相同,但现在您可以有多个错误对象,如果你需要它(而不是只有一个getLastError)。当跟踪特定错误时,这将使事情更清楚,当可能有多个错误倾向的方法连续调用。

I would go with approach number 1. This is what Apple commonly does in their APIs, such as saving files, etc. The benefits of this approach are being able to quickly see if it passed or failed (by returning a boolean), and immediately having access to the error. This is mostly the same as number 2, but you can now have more than one error object if you need it (instead of just the single "getLastError"). This will make things clearer when tracking down specific errors when there could be potentially multiple error-prone methods called in succession.