Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Taking exception with exceptions

Taking exception with exceptions
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 25, 2003, 10:44 PM
 
I can't seem to get exceptions to work properly. I have read through the Apple docs, but I keep getting a strange error. Here's what I'm doing:

Code:
-(void)getInfoFromLog:(id)sender { NSLog(@"start getInfoFromLbg"); if ([logFileDirectory isEqualToString:@"Not Entered"]) { } else { NSAutoreleasePool *logpool = [[NSAutoreleasePool alloc] init]; NS_DURING NSArray *directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath:logFileDirectory]; if (!directoryContents) { NSException* fileNotFoundException = [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil]; [fileNotFoundException raise]; } NSString *currentLogFile; ... return; NS_HANDLER if ([[localException name] isEqualToString:@"FileNotFoundException"]) { NSRunAlertPanel(@"Error Panel", @"%@", @"OK", nil, nil, localException); } //[localException raise]; /* Re-raise the exception. */ NS_ENDHANDLER }
I keep getting an this error:

'parse error before "else"' on the NS_HANDLER line.

Can someone give me a quick what's up on Cocoa exceptions. I get the basics, but just can't seem to get them to work in the real world.

kman
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 25, 2003, 10:51 PM
 
You need to enclose the bocks within the handler... like this:
Code:
NS_DURING { ... code ... } NS_HANDLER { ... more code ... } NS_ENDHANDLER
Also, I'm not sure what you've replaced with "..." in your posting, but it looks like you have mismatched {}'s. Turn on syntax-aware indenting in Project Builder -- it'll help you track down which { is missing a }.
Rick Roe
icons.cx | weblog
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 25, 2003, 11:01 PM
 
Thank you!

Why does Apple not include blocks in their docs:

Code:
NS_DURING ... if (someError) [anException raise]; ... NS_HANDLER if ([[localException name] isEqualToString:MyAppException]) { NSRunAlertPanel(@"Error Panel", @"%@", @"OK", nil, nil, localException); } [localException raise]; /* Re-raise the exception. */ NS_ENDHANDLER
kman
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 25, 2003, 11:07 PM
 
nevermind.

kman
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 26, 2003, 10:38 AM
 
I'm really struggling with this. I think I just need a little real world example. My app gets info from a file stored either locally or remotely. The problem is that sometimes the remote machine becomes unmounted which results in a crash. Can someone help me build an exception routine around this bit of code?

Code:
-(void)getInfoFromLog:(id)sender { NSLog(@"start getInfoFromLog"); if (!([logFileDirectory isEqualToString:@"Not Entered"])) { NSAutoreleasePool *logpool = [[NSAutoreleasePool alloc] init]; NSArray *directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath:logFileDirectory]; NSString *currentLogFile; NSEnumerator *enumerator = [directoryContents objectEnumerator]; id file; while ((file = [enumerator nextObject])) { if ([file rangeOfString:@"unitinfo.txt" options:NSCaseInsensitiveSearch].location != NSNotFound) currentLogFile = file; } ...
I think the exception routine needs to go around the "NSArray *directoryContents = " line. Is there a builtin exception for 'unable to read directory' or something? Is there a list of builtin exception names?

sorry to be such a newb,
kman
     
eno
Banned
Join Date: Sep 2000
Location: Fightclub
Status: Offline
Reply With Quote
May 26, 2003, 11:03 PM
 
Originally posted by Rickster:
You need to enclose the bocks within the handler... like this:
Code:
NS_DURING { ... code ... } NS_HANDLER { ... more code ... } NS_ENDHANDLER
Erm... None of my code, nor any of Apple's examples, enclose the blocks within curly brackets, and they all work fine.

I would say the brackets are entirely optional.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:35 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2