2012年5月22日 星期二

Exception Handling Using @try

refer to Addison Wesley - Programming.in.ObjectiveC.2.0.2nd (2009)

格式如下
@try {
statement
statement
...
}
@catch (NSException *exception) {
statement
statement
...
}

int main (int argc, char *argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *f = [[Fraction alloc] init];
@try {
[f noSuchMethod];
}
@catch (NSException *exception) {
NSLog(@ ” Caught %@%@ ” , [exception name], [exception reason]);
}
NSLog (@ ” Execution continues! ” );
[f release];
[pool drain];
return 0;
}

*** -[Fraction noSuchMethod]:  unrecognized selector sent to instance 0x103280
Caught NSInvalidArgumentException: *** -[Fraction noSuchMethod]:
unrecognized selector sent to instance 0x103280
Execution continues!

沒有留言: