Latest 1.0
Homepage https://github.com/maxcabral/NSError-Exception
License MIT
Frameworks Foundation
Authors

Version
Platform

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:

http://nshipster.com/nserror/

http://club15cc.com/code/objective-c/dispelling-nsexception-myths-in-ios-can-we-use-try-catch-finally

https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSException_Class/Reference/Reference.html

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/Reference/Reference.html

Usage

To run the example project; clone the repo, and run pod install from the Example directory first.

Requirements

None! Happy day! Well… NSError, but if you don’t have that you’re in bad shape.

Installation

NSError+Exception is available through CocoaPods, to install
it simply add the following line to your Podfile:

pod "NSError+Exception"

Author

Maxwell Cabral

License

NSError+Exception is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "NSError+Exception",
    "version": "1.0",
    "summary": "A handy wrapper for throwing NSErrors as NSExceptions",
    "description": "                       A handy wrapper for throwing NSErrors as NSExceptions (if you're into that kind of thing)nn                       Suppose you have the following code:nn                           NSError *error;n                           BOOL success = [[NSFileManager defaultManager] moveItemAtPath:@"/start"n                                                                                  toPath:@"/end"n                                                                                   error:&error];nn                       And error is non-nil but most of your logic deals with NSExceptions rather than NSErrors.n                       Rather than having a corner case where you need to pass in NSErrors, you can simply throw the NSErrornn                           if (!success) {n                               [error throw];n                           }nn                       Your code will either crash or be handled by your @try/@catch/@finally block.nn                       The logic used is along the lines of:nn                           @throw [NSException exceptionWithName:@"NSError" reason:self.debugDescription userInfo:@{ @"NSError" : self }];nn                       You can provide a more informative exception name by sending the - (void)throwWithName:(NSString*)name message instead.nn                       NSException/NSError information:nn                       http://nshipster.com/nserror/nn                       http://club15cc.com/code/objective-c/dispelling-nsexception-myths-in-ios-can-we-use-try-catch-finallynn                       https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSException_Class/Reference/Reference.htmlnn                       https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/Reference/Reference.htmlnn",
    "homepage": "https://github.com/maxcabral/NSError-Exception",
    "license": "MIT",
    "authors": {
        "Maxwell Cabral": "[email protected]"
    },
    "source": {
        "git": "https://github.com/maxcabral/NSError-Exception.git",
        "tag": "1.0"
    },
    "requires_arc": true,
    "source_files": "NSError+Exception.{h,m}",
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This