What are best practices for error handling when writing an API for the iphone?

前端 未结 2 1040
说谎
说谎 2021-02-05 21:37

We are writing an API for iphone developers and we don\'t know what the best practice is for exception handling. We looked into NSError, standard POSIX way, N

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 21:54

    From the Introduction to Exception Programming Topics:

    Important: You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server. You usually take care of these sorts of errors with exceptions when an application is being created rather than at runtime.

    ...

    Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications. For further information, see Error Handling Programming Guide For Cocoa.

    So as I understand it, only use exceptions when something is fatally wrong. Otherwise, use NSError objects.

提交回复
热议问题