2012-10-16 14 views
8

+[NSException exceptionWithName:reason:userInfo:] kullanmak istiyorum.NSException, Objective-C'deki istisna adı için hangi adı kullanmalıyım?

Name: bağımsız değişkeni için hangi dizeyi kullanmalıyım?

Projede istisna adı benzersiz olmalı mı?
Veya "MyException" özelliğini tüm özel durumlarım için kullanabilir miyim?

Ve istisna adının ne için kullanıldığını bilmiyorum.
Özel durum adları nerede kullanılır?

cevap

6

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

+0

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). –

1

bir göz atın, bu yolla istisna ekleyerek amacı, mümkün olduğu kadar çabuk bir sorun tespit bildirin ve tanıyı sağlamaktır. Sorunun (kaynağının yani çizgi, yöntemi) bir istisna adı projenize özgü veya belirli tercih edip Gibi

, iyi tanı bilgileri size hangi bağlıdır.

İstisna adları, istisna durumunun nereden kaynaklandığını belirlemek için uygulama tarafından bildirileceğinden, uygulamalarınız genelinde paylaşılabilir.

İlgili konular