4 ms·
The error style I've always liked best is that of Objective-C. NSError* error = nil; [object doSomething: foo error: &error]; if(error){ } I find exc
by fleitz 12y ago
The error style I've always liked best is that of Objective-C.
NSError* error = nil;
[object doSomething: foo error: &error];
if(error){
}
I find exceptions to be rather overkill for most error type problems to begin and with the ObjC system it's easy to ignore errors that may not be critical.
[object doSomething: foo error: nil];
- cnvogel 12y agoWell... using the example from the linked article, it could be made equivalent to your Objective-C snippet, provided the optional passed error-object/struct isn't updated only on errors (as the article suggests), but also can be used to find out if an error happened in the first place. With a suitable modification, the mentioned example would have to be changed from... libfoo_widget_container_t container = NULL; libfoo_error_details_t error = NULL; if (libfoo_create_widgets(12, &container, &error) != libfoo_success) { printf("Error creating widgets: %s\n", libfoo_error_details_c_str(error)); libfoo_error_details_free(error); abort(); // goodbye, cruel world! } ...to... libfoo_widget_container_t container = NULL; libfoo_error_details_t error = NULL; libfoo_create_widgets(12, &container, &error); if (LIBFOO_IS_BAD(error)) { printf("Error creating widgets: %s\n", libfoo_error_details_c_str(error)); libfoo_error_details_free(error); abort(); // goodbye, cruel world! } where LIBFOO_IS_BAD() would be a macro or inline function inspecting the error object/struct, and might be something like #define LIBFOO_IS_BAD(error) ((error) && (error)->errno != 0) Cutting out the cruft, it's basically what you had suggested to pre pretty Objective-C style ;-) /* your example */ NSError* error = nil; [object doSomething: foo error: &error]; if(error){ } /* article */ libfoo_error_details_t error = NULL; libfoo_create_widgets(12, &container, &error); if (LIBFOO_IS_BAD(error)) { } EDIT: The linked article already mentions that in the example, error would only be updated if the function returned something unlinke libfoo_success, so one could directly convert to your favourite style keeping these semantics!