2012-10-16 3 views

Antwort

6

Sie können den Namen in @catch (NSException *theErr) verwenden.

@catch (NSException *theErr) 
{ 
    tst_name = [theErr name]; 
    if ([tst_name isEqualToString:@"name"]) 
} 

welche Zeichenfolge sollte ich für Argument Namen verwenden :?

Alles sinnvoll.

Sollte der Name der Ausnahme im Projekt eindeutig sein?

Nr

Oder kann ich @ "MyException" für alle meine Ausnahme?

Ja, aber Sie sollten aussagekräftige Namen verwenden.

- (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]); 
} 

Werfen Sie einen Blick auf Understanding Exceptions and Handlers in Cocoa

+0

danke für deine antwort! der Link ist auch nützlich. Der Autor empfiehlt uns, [vordefinierte Ausnahmenamen] (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html), z. B. NSRangeException, auf [page2 ] (http://macdevcenter.com/pub/a/mac/2007/07/31/understanding-exceptions-and-handlers-in-cocoa.html?page=2). –

1

Letztlich ist es die Absicht der Zugabe einer Ausnahme auf diese Weise ein Problem so schnell wie möglich zu erkennen, melden Sie es und Diagnose zu ermöglichen.

Daher hängt es davon ab, ob Sie einen Ausnahmebedingungsnamen auswählen, der für Ihr Projekt eindeutig ist oder für das Problem spezifisch ist (z. B. Quelle, Methode).

Ausnahmebezeichnungen können in Ihren Apps geteilt werden, da sie von der App gemeldet werden, um festzustellen, woher die Ausnahme stammt.

Verwandte Themen