How to handle errors on iPhone

I have a question about error / exception handling on iPhone.

I read the documentation about the exception, and it seems that exceptions can only be used for exceptional situations.

Does this mean that you cannot use them, as in java?

for example, I'm trying to write a usage controller for my application. I have several Java examples from previous projects in this language that use exceptions in case of errors.

The question posed is simple: can I follow the example I got in Java and "translate" it into Objective-C (and use Objective-C exceptions), or is there a better way to do this?

here is the code I would like to make Objective-C friendly:

public void addPerformance(Perfomance perf) {
        //do some preparation
            ...
    //execute the usecase
        executor(new AddPerformance(perf));
}

    private void executor(Usecase usecase) {

            try {
                UnitOfWorkServices.INSTANCE.bizTransactionStart();
                usecase.execute();
UnitOfWorkServices.INSTANCE.bizTransactionCommit();
            } catch (RealException re) {
                UnitOfWorkServices.INSTANCE.bizTransactionEscape();
                throw re;
            } catch (Exception e) {
                UnitOfWorkServices.INSTANCE.bizTransactionEscape();
                throw new FatalException(this.getClass().getName() + " / executor("
                        + usecase.getClass().getSimpleName() + ")", e,
                        "APPXCP_006_UNEXPECTED_EXCEPTION",
                        "\n\t |*| : Unexpected exception translated into FatalException");
            } finally {
                UnitOfWorkServices.INSTANCE.bizTransactionEnd();
            }
        }

.

,

+5
2

, , try/catch Objective-C . throw re;, iPhone .

, , iPhone SDK , , API, , , , , NSError, , , . , :

NSError* error = nil;
[[UnitOfWorkServices sharedInstance] bizTransactionStart];
bool success = [usecase execute:&error];
if (success) {
    [[UnitOfWorkServices sharedInstance] bizTransactionCommit];
}
else if (error) {
    int code = [error code];
    [[UnitOfWorkServices sharedInstance] bizTransactionEscape];
    if (code == MY_MINOR_ERROR_CODE) {
        //do something
    }
    else if (code == MY_FATAL_ERROR_CODE) {
        //do something else
    }
    else {
        //handle unexpected error type(s)
    }
}
else {
    //operation failed with no specific error details
    [[UnitOfWorkServices sharedInstance] bizTransactionEscape];
    //handle generic error
}
[[UnitOfWorkServices sharedInstance] bizTransactionEnd];
+3

, Java, . Java Objective-C , . , ; , .

"Cocoa way" iOS , API: s.

: http://blog.jayway.com/2010/10/13/exceptions-and-errors-on-ios/

:

, . , - , , 100 , . , .

NSError - , , , , . , IO-, , . , , - .

+1

All Articles