问题
I'm using selectors to dynamically call functions in Objective-C. My problem is that my @try
/@catch
block is not catching the exception if I pass an non-existent function name.
In the console I get an uncaught NSInvalidArgumentException
and further down I get an uncaught NSException
.
I have tried catching the the two types of exceptions in a standard @try
/@catch
block but they aren't registering. Xcode won't let me compile if I try to catch NSInvalidArgumentException
as it's not a recognized type.
Any ideas on how to solve this?
@try {
SEL s = NSSelectorFromString(@"funName");
[self performSelector:s withObject: nil];
}
@catch (NSException/NSInvalidArgumentException exception) {
NSLog(@"ERROR: %@", [exception message/reason];
}
回答1:
Many consider this to be a bug.
See http://openradar.appspot.com/8081169
By testing similar code, I can make it work on a device but not in the simulator.
来源:https://stackoverflow.com/questions/6412263/nsinvalidargumentexception-not-being-caught-in-code