Sen @catch (NSException *theErr)
in adı kullanabilirsiniz.
@catch (NSException *theErr)
{
tst_name = [theErr name];
if ([tst_name isEqualToString:@"name"])
}
Ben argüman Adı için kullanmak gerektiğini dize
:? Anlamlı
herhangi bir şey.
istisna adı projede benzersiz olmalı?
sayılı
Ya kullanabilirsiniz @ "MyException" benim istisna tümü için?
Evet, ama anlamlı adlar kullanmalıdır.
- (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]);
}
Sonuçta
Understanding Exceptions and Handlers in Cocoa
Cevabınız için teşekkürler! bağlantı da yararlıdır. yazarı, [önceden tanımlanmış istisna isimleri] 'ni (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html), örneğin NSRangeException' ı kullanmamızı tavsiye eder. ] (http://macdevcenter.com/pub/a/mac/2007/07/31/understanding-exceptions-and-handlers-in-cocoa.html?page=2). –