Reputation: 156
how to soive this to stop warning message for this? am trying to put error on label. Does try catch really prevent crashing app?
@catch (NSException *ex) {
errorLbl.text =ex;
}
Upvotes: 0
Views: 302
Reputation: 17898
NSException's reason
contains a "human readable" reason that you could display, like:
@catch (NSException *ex) {
errorLbl.text = ex.reason;
}
See the NSException reference for more info.
It's worth noting that exceptions in Objective-C (unlike in other languages) are intended to be used for programming or unexpected runtime errors, not normal program flow. The docs state:
Important: You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server. You usually take care of these sorts of errors with exceptions when an application is being created rather than at runtime.
...
Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications. For further information, see Error Handling Programming Guide.
See the Error Handling Programming Guide.
Upvotes: 0
Reputation: 50697
Instead of trying to catch a crash, you should make sure that code will not crash altogether. However, you can always convert the NSException
to NSString
@catch (NSException *ex) {
errorLbl.text = [NSString stringWithFormat:@"%@",[ex reason]];
}
NSException
@interface NSException : NSObject <NSCopying, NSCoding> {
@private
NSString *name;
NSString *reason;
NSDictionary *userInfo;
id reserved;
}
Upvotes: 2
Reputation: 7541
This question is hard to understand, but if your asking, will that Catch catch every exception that is based off of a NSException, then the answer is yes, with a small issue.
You can catch it, but since your not doing anything about it, the code will continue after the catch. If your app is crashing, then what will happen is you will fill some label with the error, but it wont mean the app is in a stable position, it might just keep crashing.
Upvotes: 1