Creating and Raising an NSException Object

+ exceptionWithName:reason:userInfo:

Creates and returns an exception object .

Example

NSException *exception = [NSException exceptionWithName:@"Custom Exception" reason:@"Custom Reason" userInfo:@{@"Localized key": @"Unexpected Input"}];
NSLog(@"%@",exception);
		

Output

2014-04-12 05:48:25.638 iOS-Tutorial[1734:a0b] Custom Reason
		

+ raise:format:

A convenience method that creates and raises an exception.

Example

@try {
    [NSException raise:@"Custom Exception" format:@"Custom Reason"];
}
@catch (NSException *exception) {
    NSLog(@"%@",exception);
}
@finally {
        
}		

Output

2014-04-12 05:59:01.031 iOS-Tutorial[1828:a0b] Custom Reason
		

- initWithName:reason:userInfo:

Initializes and returns a newly allocated exception object.

Example

NSException *exception = [[NSException alloc ]initWithName:@"Custom Exception" reason:@"Custom Reason" userInfo:@{@"Localized key": @"Unexpected Input"}];
NSLog(@"%@",exception);
		

Output

2014-04-12 05:49:49.471 iOS-Tutorial[1752:a0b] Custom Reason
		

- raise

Raises the receiver, causing program flow to jump to the local exception handler.

Example

@try {
    NSException *exception = [[NSException alloc ]initWithName:@"Custom Exception" reason:@"Custom Reason" userInfo:@{@"Localized key": @"Unexpected Input"}];
    [exception raise];
}
@catch (NSException *exception) {
    NSLog(@"%@",exception);
}
@finally {
        
}		

Output

2014-04-12 05:53:35.918 iOS-Tutorial[1804:a0b] Custom Reason
		

Querying an NSException Object

- name

Returns an NSString object used to uniquely identify the receiver.

Example

NSException *exception = [[NSException alloc ]initWithName:@"Custom Exception" reason:@"Custom Reason" userInfo:@{@"Localized key": @"Unexpected Input"}];
NSLog(@"%@",exception.name);
		

Output

2014-04-12 05:50:57.729 iOS-Tutorial[1777:a0b] Custom Exception
		

- reason

Returns an NSString object containing a “human-readable” reason for the receiver.

Example

NSException *exception = [[NSException alloc ]initWithName:@"Custom Exception" reason:@"Custom Reason" userInfo:@{@"Localized key": @"Unexpected Input"}];
NSLog(@"%@",exception.reason);
		

Output

2014-04-12 05:50:37.527 iOS-Tutorial[1765:a0b] Custom Reason
		

- userInfo

Returns an NSDictionary object containing application-specific data pertaining to the receiver.

Example

NSException *exception = [[NSException alloc ]initWithName:@"Custom Exception" reason:@"Custom Reason" userInfo:@{@"Localized key": @"Unexpected Input"}];
NSLog(@"%@",exception.userInfo);
		

Output

2014-04-12 05:51:23.822 iOS-Tutorial[1789:a0b] {
    "Localized key" = "Unexpected Input";
}
		

Getting Exception Stack Frames

- callStackReturnAddresses

Returns the call return addresses related to a raised exception.

Example

@try {
    [NSException raise:@"Custom Exception" format:@"Custom Reason" arguments:"test,test2"];
}
@catch (NSException *exception) {
    NSLog(@"%@",[exception callStackReturnAddresses]);
}
@finally {
   		

Output

2014-04-12 06:03:01.351 iOS-Tutorial[1874:a0b] (0x17395c8 0x14bc8b6 0x1739448 0x2693 0x3429a8 0x342c44 0x26b5ad 0x26b947 0x26bbdd 0x27644a 0x2298e0 0x22dfb8 0x24242c 0x242999 0x22fc35 0x368c2eb 0x368bdf6 0x16b4dd5 0x16b4b0b 0x16df7ec 0x16deb33 0x16de94b 0x22d6ed 0x22f94b 0x361d 0x1d75725)
		

- callStackSymbols

Returns an array containing the current call symbols.

Example

@try {
    [NSException raise:@"Custom Exception" format:@"Custom Reason"];
}
@catch (NSException *exception) {
    NSLog(@"%@",[exception callStackSymbols]);
}
@finally {
        
}		

Output

2014-04-12 06:03:51.377 iOS-Tutorial[1912:a0b] (
	0   CoreFoundation                      0x017395e4 __exceptionPreprocess + 180
	1   libobjc.A.dylib                     0x014bc8b6 objc_exception_throw + 44
	2   CoreFoundation                      0x017393bb +[NSException raise:format:] + 139
	3   iOS-Tutorial                        0x000026ba -[ViewController viewDidLoad] + 266
	4   UIKit                               0x003429a8 -[UIViewController loadViewIfRequired] + 696
	5   UIKit                               0x00342c44 -[UIViewController view] + 35
	6   UIKit                               0x0026b5ad -[UIWindow addRootViewControllerViewIfPossible] + 66
	7   UIKit                               0x0026b947 -[UIWindow _setHidden:forced:] + 312
	8   UIKit                               0x0026bbdd -[UIWindow _orderFrontWithoutMakingKey] + 49
	9   UIKit                               0x0027644a -[UIWindow makeKeyAndVisible] + 65
	10  UIKit                               0x002298e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
	11  UIKit                               0x0022dfb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
	12  UIKit                               0x0024242c -[UIApplication handleEvent:withNewEvent:] + 3447
	13  UIKit                               0x00242999 -[UIApplication sendEvent:] + 85
	14  UIKit                               0x0022fc35 _UIApplicationHandleEvent + 736
	15  GraphicsServices                    0x0368c2eb _PurpleEventCallback + 776
	16  GraphicsServices                    0x0368bdf6 PurpleEventCallback + 46
	17  CoreFoundation                      0x016b4dd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
	18  CoreFoundation                      0x016b4b0b __CFRunLoopDoSource1 + 523
	19  CoreFoundation                      0x016df7ec __CFRunLoopRun + 2156
	20  CoreFoundation                      0x016deb33 CFRunLoopRunSpecific + 467
	21  CoreFoundation                      0x016de94b CFRunLoopRunInMode + 123
	22  UIKit                               0x0022d6ed -[UIApplication _run] + 840
	23  UIKit                               0x0022f94b UIApplicationMain + 1225
	24  iOS-Tutorial                        0x0000363d main + 141
	25  libdyld.dylib                       0x01d75725 start + 0
)
		

Advertisements