In this recipe, we will learn how to handle runtime errors within an iOS application.
In this section, we will learn how we can handle runtime errors and learn how we can improve error handling using the @try…@catch…@finally
clauses to prevent our application from crashing.
To begin, follow the following simple steps as outlined:
Open the
MemoryLeaks.xcodeproj
project file.Open the
ViewController.m
implementation file from the Project Navigator window.Modify the
viewDidLoad
method as shown by the highlighted code sections in the following code snippet:- (void)viewDidLoad { [super viewDidLoad]; [self causeRuntimeError]; }
Next, create the
causeRuntimeError
method as shown in the following code snippet:-(void)causeRuntimeError { NSLog(@"Runtime Error section started"); NSMutableArray *myArray = [NSMutableArray array]; for (int x = 0; x < 30; x++) { // Add the value to our Array [myArray addObject:[NSNumber numberWithInt...