On Tuesday, 3 June 2014 at 20:26:52 UTC, Jacob Carlborg wrote:
On 2014-06-03 01:01, deadalnix wrote:

How do they do error handling ?

Objective-C does support exceptions, but libraries like Cocoa avoids throwing exceptions and leave those to the user (developer). Instead it usually returns a bool to indicate success or failure and then provides an NSError instance via an out parameter.

Writing messages which set errors like that was always weird.

// The pointer needs to use __autoreleasing so it captures the correct
// reference semantics.
- (BOOL) doSomethingWithError:(NSError* __autoreleasing * error) {
// You must set to nil, as Objective C won't initialise to nil implicitly.
    NSError* otherError = nil;

    // Pass pointer to object in a C-like way.
    [foo doSomethingElseWithError:&otherError];

    // We have to check that the out param is not nil.
    if (error && otherError) {
        *error = otherError;

        // We return a boolean flag for those people who
        // don't capture the error itself but want a convenient
        // if([foo doSomethingWithError:nil] == NO)
        return NO;
    }

    return YES;
}

What does this look like if you use exceptions instead in a saner language?

void doSomething() {
    doSomethingElse();
}

Oh! Right!

Reply via email to