 |
 |
Taking exception with exceptions
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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 }.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Banned
Join Date: Sep 2000
Location: Fightclub
Status:
Offline
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|