A handy wrapper for throwing NSErrors as NSExceptions (if you're into that kind of thing)
Suppose you have the following code:
NSError *error;
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:@"/start"
toPath:@"/end"
error:&error];
And error is non-nil but most of your logic deals with NSExceptions rather than NSErrors. Rather than having a corner case where you need to pass in NSErrors, you can simply throw the NSError
if (!success) {
[error throw];
}
Your code will either crash or be handled by your @try/@catch/@finally block.
The logic used is along the lines of:
@throw [NSException exceptionWithName:@"NSError" reason:self.debugDescription userInfo:@{ @"NSError" : self }];
You can provide a more informative exception name by sending the - (void)throwWithName:(NSString*)name message instead.
NSException/NSError information:
To run the example project; clone the repo, and run pod install
from the Example directory first.
None! Happy day! Well... NSError, but if you don't have that you're in bad shape.
NSError+Exception is available through CocoaPods, to install it simply add the following line to your Podfile:
pod "NSError+Exception"
Maxwell Cabral
NSError+Exception is available under the MIT license. See the LICENSE file for more info.