views:

114

answers:

1

I had this in my try-catch-finally and it compiles ok.

@catch (NSException *e) {
.....
}

...but when I tried to catch NSRangeException: I got error.

@catch (NSRangeException *ne) {
.....
}

The error is:

Expected declaration specifiers: NSRangeExpection

(TechZen says -- this presumably a typo for:)

Expected declaration specifiers: NSRangeException

I did later to import "Funcation/NSException.h", but it didn't remove the error.

+1  A: 

This error can be caused nesting errors in the code preceding the line. It may have nothing to do with the line itself. Check and/or post the code above the line where the error occurs.

If the error message you posted is the actual message you got back, then you have a simple typo of writing NSRangeExpected for NSRangeException.


Update:

Okay, looking over it again, the answer is simple and I just missed it. NSException is a class but NSRangeException is a name defined in a string constant:

extern NSString *NSRangeException;

... which simply returns a string of "NSRangeException".

Obviously, the @catch is expecting to receive a NSException object but gets a NSString object instead.

You can setup a @catch to accept a string like this:

@catch(NSString *stringException){...

or

@catch(id idException){...

... but usually there is no reason to.

You use NSRangeException and the other exception names to test against the name of the NSException instance that the @catch catches.

TechZen
I changed nothing but NSException to NSRangeException. That's why I ruled out the possible of preceding lines causing the problem.Yes, it was a typo.I have changed my code so I no longer have the source.
Martin
I get the same error - just change NSException to NSRangeException makes the exact same problem ("Expected declaration specifiers or '...' before 'NSRangeException')
Jonny
Yes, I made a careless mistake. I missed something simple and I apologize. See my update for the correct answer.
TechZen