ios - throwing custom exception in objective c -
i have following code . . .
@try { nsarray * array = [[nsarray alloc] initwithobjects:@"1",@"2",nil]; // below code raise exception [array objectatindex:11]; } @catch(nsexception *exception) { // want create custom exception , throw . nsexception * myexception = [[nsexception alloc] initwithname:exception.name reason:exception.reason userinfo:exception.userinfo]; //now saving callstacksymbols mutable array , adding objects nsmutablearray * mutablearray = [[nsmutablearray alloc] initwitharray:exception.callstacksymbols]; [mutablearray addobject:@"object"]; //but problem when try assign mutable array myexception getting following error myexception.callstacksymbols = (nsarray *)mutablearray; //error : no setter method 'setcallstacksymbols' assignment property @throw myexception; }
please fix , wanted add objects callstacksymbols . . . .thanks in advance
if coming java background, exception handling in objective-c feels strange @ first. in fact don't use nsexception
own error handling. use nserror
instead, can find @ many other points through sdk, when handling unexpected error situations (e.g. url operations).
error handling done (roughly) this:
write method takes pointer nserror parameter...
- (void)dosomethingthatmaycauseanerror:(nserror*__autoreleasing *)anerror { // ... // failure situation nsdictionary tuserinfo = @{@"mycustomobject":@"customerrorinfo"}; nserror* terror = [[nserror alloc] initwithdomain:@"mydomain" code:123 userinfo:tuserinfo]; anerror = terror; }
the userinfo dictionary place put whatever information needs provided error.
when calling method, check error situation this...
// ... nserror* terror = nil; [self dosomethingthatmaycauseanerror:&terror]; if (terror) { // error occurred! nsstring* tcustomerrorobject = [terror.userinfo valueforkey:@"mycustomobject"]; // ... }
if calling sdk method result in "nserror != nil
", can add own information userinfo dictionary , pass error caller shown above.
Comments
Post a Comment