When writing code, the most afraid of code to write hundreds of thousands of lines, but a running program crashes, in order to improve the robustness of the Code, the following provides a way to improve the robustness of the code:
Exception trapping mechanism
1 // exception capture mechanism: Improve code robustness 2 @try---@catch---@finally
The following is a small demo of a printed array to illustrate its usage
1Nsarray *arr = @[@Ten, @ -, @ -];2 @try {3 /*put code that is likely to cause a problem that causes the program to crash in the try statement body*/4NSLog (@"arr[3] =%@", arr[3]);5 }6 @catch(NSException *exception) {7 /*If an exception occurs, the program does not crash, jumps to the catch statement body, and can add some hints when an exception occurs in the body of the catch statement*/8NSLog (@"exception:%@", exception);9 }Ten @finally { One /*the contents of the finally statement body will be executed regardless of the presence of a wood.*/ ANSLog (@"finally: The anomaly mechanism is over!"); -}
In the code above, the response is annotated with code that shows how the exception capture mechanism works and how to improve the robustness of the code.
Here is the result of the operation:
Exception trapping mechanism