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:
MemoryLeaks.xcodeproj
project file.ViewController.m
implementation file from the Project Navigator window.viewDidLoad
method as shown by the highlighted code sections in the following code snippet:- (void)viewDidLoad
{
[super viewDidLoad];
[self causeRuntimeError];
}
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:x]]; } // This will cause a Runtime error, as we don't have any // item at this index within the array. [myArray removeObjectAtIndex:30]; NSLog(@"Runtime Error section completed."); }
You will notice that once the program executes, an exception is raised and the code execution stops. The following screenshot shows the exception error details within the Xcode IDE:
In this recipe, we created a new method causeRuntimeError
that allocated an array to hold the maximum capacity of 30 items. We then cycled through the loop and added the integer values from 0 to 29 into this array. Once, all items have been added, the loop exits and we then attempt to remove an item at the 31st position within the array.
Since we don't have an object stored at the 31st position within our array, this results in the code execution halting, causing the exception error information display as shown in the previous screenshot.
Objective-C provides you with a fail-safe method that can help you handle exception errors that can prevent your application from falling over. Keep in mind, this will still cause an error, but the exception error would have been handled by your application, preventing your app from crashing, and allowing your code to continue executing.
An exception is a special condition that interrupts the normal flow of program execution. There are a variety of reasons why an exception may be generated (exceptions are typically said to be raised or thrown), by hardware as well as software.
Examples include arithmetical errors such as division by zero, calling undefined instructions (such as attempting to invoke an unimplemented method), and attempting to access a collection element out of bounds.
Objective-C exception support involves four compiler directives: @try
, @catch
, @throw
, and @finally
, which are explained in the following table:
Let's take a look at how we can modify our
causeRuntimeError
method to take advantage of these compiler directives, and prevent our code from crashing:
causeRuntimeError
method as shown by the highlighted code sections 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:x]]; } // This will cause a Runtime error, as we don't have any // item at this index within the array. @try { [myArray removeObjectAtIndex:30]; } @catch (NSException *exception) { NSLog(@"Error: %@", exception); } @finally { NSLog(@"Runtime Error section completed."); } }
You will notice that once the program executes, the exception is raised and caught by the @catch
code block, and the exception error message is logged to the console window, and the code execution continues normally.