2012-10-16 10 views
8

मैं +[NSException exceptionWithName:reason:userInfo:] का उपयोग करना चाहता हूं।उद्देश्य-सी में, एनएसईएक्सप्शन के अपवाद नाम के लिए मुझे किस नाम का उपयोग करना चाहिए?

लेकिन तर्क Name: के लिए मुझे किस स्ट्रिंग का उपयोग करना चाहिए?

क्या अपवाद नाम परियोजना में अद्वितीय होना चाहिए?
या मैं अपने सभी अपवादों के लिए @ "MyException" का उपयोग कर सकता हूं?

और मुझे नहीं पता कि किस अपवाद का नाम उपयोग किया जाता है।
अपवाद नाम कहां उपयोग किए जाते हैं?

उत्तर

6

आप @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

+0

आपके उत्तर के लिए धन्यवाद! लिंक भी उपयोगी है। लेखक हमें [पूर्वनिर्धारित अपवाद नाम] (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)। –

1

अंततः पर एक नजर डालें, इस तरह से कोई अपवाद जोड़ने के इरादे, जितनी जल्दी हो सके एक समस्या का पता लगाने में रिपोर्ट करें और निदान अनुमति है।

इस प्रकार, चाहे आप अपनी परियोजना के लिए अद्वितीय अपवाद नाम चुनते हैं, या समस्या के लिए विशिष्ट (यानी स्रोत, विधि की रेखा) निर्भर है जिस पर आपको सबसे अच्छी नैदानिक ​​जानकारी प्रदान की जाएगी।

अपवाद नाम आपके ऐप्स पर साझा किए जा सकते हैं, क्योंकि उन्हें ऐप द्वारा रिपोर्ट किया जाएगा कि यह पहचानने के लिए कि अपवाद कहां से उत्पन्न हुआ था।

संबंधित मुद्दे

 संबंधित मुद्दे