how to report an error in this function!

Discussion in 'Mac Programming' started by saleh.hi.62, Nov 9, 2011.

  1. saleh.hi.62 macrumors member

    Joined:
    Jul 25, 2011
    #1
    here is my function, i have 3 conditions to return the requested value back. aside from the message that i print out; i want to know how i can illuminate that what exactly happened when i had a new request to my program, so the behavior of my program is related to the response.

    i tried to use "return" command with different numbers, i felt its awkward! is there any better solution?


    Code:
    -(NSString*) nextRequest{
    	
    	
    	if([webSiteQueue count]==0) {
    		NSLog(@"website queue is empty!");
    		return 1;
    	}
    	if ([webPageQueue count]==0) {
    		NSLog(@"no link is available! insert new links.");
                    return 2;
    	}
    	if([webPageQueue count]<=linkCounter+1){
    		NSLog(@"%@'s links are Crawled!",[webSiteQueue objectAtIndex:siteCounter]);
    		return 3;
    	}
    	return [webPageQueue objectAtIndex:linkCounter++];
    	
    }
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You have declared the return type to be a pointer to a NSString. Why are you trying to return integers?

    The better solution here is to return null and have a NSError ** parameter that gets updated on error: this is the pattern Apple use.
     
  3. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #3
    yes i think so, could you plz help me how i can implement it on my code above? i am confuse how to use that!
     
  4. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #5
    i studied NSError article, but i am confused!
    imagine the above function is a part of a class. then i make an instance of that object. and call the method like this.
    [myobject nextRequest];

    so if everything is okay it return a string for me. but if there is an Error how i can i get it here? because the process of my program changes and goes to different way if one of the condition is not true!
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    Look more closely at the Creating and Returning NSError Objects section. Look very carefully at the Listing 2-7.

    Also look very closely at Listing 2-1 in the Handling Error Objects Returned From Methods section.
     
  6. gnasher729, Nov 10, 2011
    Last edited: Nov 10, 2011

    gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    Open the NSFileManager documentation or header file. Plenty of methods in there following the error handling pattern. Copy what they are doing. Do you really need someone to take you by the hand and do every single step for you?

    For a really, really sad story about education, teaching and learning, written by extraordinary genius Richard Feynman, check this website: http://v.cx/2010/04/feynman-brazil-education

    Admittedly, people asking for help here are one big step ahead of the students described on that site.
     
  7. saleh.hi.62, Nov 10, 2011
    Last edited: Nov 10, 2011

    saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #8
    well, i already found out how to use that, but there is only one thing is left! i return "nil" when there is an error, but in my program where i called this method of my class i want to access to the CODE of NSError! HOW CAN I ACCESS?
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    The NSError is not the return value. You'd create a new one and return a pointer to the NSError* in a variable you've passed in. When the function returns nil indicating an error, you'd check the value in the NSError that was returned via the pointer variable passed in.

    -Lee
     
  9. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #10
    exactly, what i return is nil ! so this is my problem! how can i access through that pointer? could you plz help me with that ?
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    Post your code. It's difficult to analyze code only by its description.

    You can't access anything through a nil pointer. If you think you can, you're mistaken.

    If you can't see how to return an NSError object, then you haven't read the Error Handling Guide that was previously linked to. If you did read it, then you should read it again. Then carefully look at the listings previously mentioned (Listings 2-1 and 2-7).

    Is your code using an arg with type (NSError**) or not? If we could see your code, we wouldn't have to ask that question. If it's not passing an NSError**, then you've completely missed the point of the Error Handling Guide and its specifically mentioned listings.
     
  11. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #12
    this is function of my class.

    Code:
    -(id) nextRequest:(NSError**)error{
    	
    	
    	if([webSiteQueue count]==0) {
    		//NSLog(@"website queue is empty!");
    		NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
    		[errorDetail setValue:@"website queue is empty!" forKey:NSLocalizedDescriptionKey];
    		NSError *underlyingError = [[[NSError alloc] initWithDomain:NSPOSIXErrorDomain
    															   code:20 userInfo:errorDetail] autorelease];
    		return nil;
    	}
    	
    	if([webPageQueue count]<=linkCounter+1){
    		NSLog(@"%@'s links are Crawled!",[webSiteQueue objectAtIndex:siteCounter]);
    		NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
    		[errorDetail setValue:@"%@'s links are Crawled!" forKey:NSLocalizedDescriptionKey];
    		*error = [NSError errorWithDomain:@"myDomain1" code:200 userInfo:errorDetail];
    		return nil;
    	}
    	return [webPageQueue objectAtIndex:linkCounter++];
    	
    }
    
     
  12. chown33, Nov 10, 2011
    Last edited: Nov 10, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    Also post the code that calls it. We need to see how you're using it.


    Your nextRequest: method doesn't always store the created NSError* at the location pointed to by the 'error' arg. In particular, the body of the first 'if' statement isn't doing this.

    If you don't do this, then the caller doesn't get an NSError* when [webSiteQueue count] is 0.

    Reread the two listings previously mentioned, in particular Listing 2-7.


    I don't understand why you use errorWithDomain:code:userInfo: in one case, but alloc and initWithDomain:code:userInfo: in the other. Simplify your code, make it consistent, and don't add needless variation. Especially when doing so creates avoidable bugs.
     
  13. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #14
    here is the command:
    Code:
    sch *ss=[[sch alloc]init];
    
    NSLog(@"%@",[ss nextRequest:nil]);
     
  14. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #15
    You're passing in nil. How do you expect that to work! I throw my hands up in the air.
     
  15. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #16
    then how should i use that ? !

    at first my code was this:

    Code:
    [ss nextRequest];
    but it errors!
     
  16. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #17
    Here, I'll quote listing 2-1 of the Error Handling Programming Guide and highlight the really important parts for you in red.

    Code:
    [COLOR=red]NSError *theError = nil;[/COLOR]
    BOOL success = [myDoc writeToURL:[self docURL] ofType:@"html" error:[COLOR=red]&theError[/COLOR]];
     
    if (success == NO) {
        // Maybe try to determine cause of error and recover first.
        NSAlert *theAlert = [NSAlert alertWithError:theError];
        [theAlert runModal]; // Ignore return value.
    }
    
    Now this sample is setup for a method that returns YES on success or NO on error. Your method doesn't do that, it returns nil on error. So how do you think that will change the if?
     

Share This Page