आप @catch (NSException *theErr)
में नाम का उपयोग कर सकते हैं।
@catch (NSException *theErr)
{
tst_name = [theErr name];
if ([tst_name isEqualToString:@"name"])
}
क्या स्ट्रिंग मैं तर्क नाम के लिए उपयोग करना चाहिए :?
कुछ सार्थक।
क्या अपवाद नाम परियोजना में अद्वितीय होना चाहिए?
सं
या मैं "MyException" का उपयोग कर सकते @ मेरी अपवाद के सभी के लिए?
हां, लेकिन आपको सार्थक नामों का उपयोग करना चाहिए।
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSNumber *tst_dividend, *tst_divisor, *tst_quotient;
// prepare the trap
@try
{
// initialize the following locals
tst_dividend = [NSNumber numberWithLong:8];
tst_divisor = [NSNumber numberWithLong:0];
// attempt a division operation
tst_quotient = [self divideLong:tst_dividend by:tst_divisor];
// display the results
NSLog (@"The answer is: %@", tst_quotient);
}
@catch (NSException *theErr)
{
// an exception has occured
// display the results
NSLog (@"The exception is:\n name: %@\nreason: %@"
, [theErr name], [theErr reason]);
}
@finally
{
//...
// the housekeeping domain
//...
}
}
- (NSNumber *)divideLong:(NSNumber *)aDividend by:(NSNumber *)aDivisor
{
NSException *loc_err;
long loc_long;
// validity check
loc_long = [aDivisor longValue];
if (loc_long == 0)
{
// create and send an exception signal
loc_err = [NSException
exceptionWithName:NSInvalidArgumentException
reason:@"Division by zero attempted"
userInfo:nil];
[loc_err raise]; //locate nearest exception handler,
//If the instance fails to locate a handler, it goes straight to the default exception handler.
}
else
// perform the division
loc_long = [aDividend longValue]/loc_long;
// return the results
return ([NSNumber numberWithLong:loc_long]);
}
Understanding Exceptions and Handlers in Cocoa
स्रोत
2012-10-16 12:08:19
आपके उत्तर के लिए धन्यवाद! लिंक भी उपयोगी है। लेखक हमें [पूर्वनिर्धारित अपवाद नाम] (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html) का उपयोग करने की सिफारिश करता है, उदाहरण के लिए NSRangeException, [पेज 2 पर) ] (http://macdevcenter.com/pub/a/mac/2007/07/31/understanding-exceptions-and-handlers-in-cocoa.html?page=2)। –