As I was developing iOS applications, one problem came out when I tried to handle runtime error in the application.
The problem can be described as follows:
Here we have three functions:
- (void)A;
- (void)B;
- (void)C;
in their implementations, A calls B, while B calls C.
During the runtime, application may receive wrong inputs from users, for example an invalid string or number.
Of course, stopping the application will be a solution, but it is quite bad user experience.
Here I learnt to use Exception mechanism to handle such runtime error.
In function A, or somewhere else you want your program to jump when error is detected, write like this:
@try{
[self B]
@catch (NSException *exception){
//do something using exception
}
While in function where error is detected:
if(ERROR)
@throw [NSException exceptionWithName:@"error title" reason:@"This is error reason" userInfo:nil];
In this way, if user happens to enter an invalid input which causes error in C, the program will then be directed back to A without any complex return.
P.S. It seems that setjmp and longjmp are no longer supported in Objective-C since its 2.0 version.
Recent Comments