vb.net barcode font 12: Exceptions, Signals, Errors, and Debugging in Java

Creating Data Matrix ECC200 in Java 12: Exceptions, Signals, Errors, and Debugging

CHAPTER 12: Exceptions, Signals, Errors, and Debugging
Data Matrix 2d Barcode Generator In Java
Using Barcode generation for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
DataMatrix Creator In Java
Using Barcode creator for Android Control to generate, create ECC200 image in Android applications.
www.OnBarcode.com
application, you may want to track down the root cause (where did the nil pointer come from, and is nil a valid value for that variable ), but in this case we ll work around the problem by putting a safety check before adding the object, like this:
Printing EAN-13 Supplement 5 In Java
Using Barcode creator for Android Control to generate, create EAN13 image in Android applications.
www.OnBarcode.com
Making QR Code ISO/IEC18004 In Java
Using Barcode creation for Android Control to generate, create QR Code JIS X 0510 image in Android applications.
www.OnBarcode.com
if (object2 != nil) { [array addObject:object2]; }
Draw GTIN - 128 In Java
Using Barcode drawer for Android Control to generate, create GS1-128 image in Android applications.
www.OnBarcode.com
Create Barcode In Java
Using Barcode maker for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
That takes care of that. NSInvalidArgumentException is one of the most frequently encountered exceptions in Cocoa, and you ve just seen the two most frequent situations that trigger it: calling a method on a class which doesn t implement that method, and trying to insert nil into an array. Build & Run the app again, and get ready for the next problem.
Code39 Generator In Java
Using Barcode drawer for Android Control to generate, create Code 3/9 image in Android applications.
www.OnBarcode.com
Draw ANSI/AIM I-2/5 In Java
Using Barcode drawer for Android Control to generate, create Uniform Symbology Specification ITF image in Android applications.
www.OnBarcode.com
NSRangeException
ECC200 Creator In None
Using Barcode creator for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
Paint ECC200 In .NET Framework
Using Barcode generator for .NET framework Control to generate, create ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
The previous exceptions have been cleared up, but look what happens now:
Create Data Matrix In None
Using Barcode drawer for Online Control to generate, create Data Matrix image in Online applications.
www.OnBarcode.com
ANSI/AIM Code 39 Drawer In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create Code39 image in Visual Studio .NET applications.
www.OnBarcode.com
2009-09-16 23:44:33.038 ExceptionCity[8881:10b] 2009-09-16 23:44:33.065 ExceptionCity[8881:10b] 2009-09-16 23:44:33.066 ExceptionCity[8881:10b] 2009-09-16 23:44:33.066 ExceptionCity[8881:10b] (2147483647( or possibly larger)) beyond bounds Total length of all name components: 15 inserted all the objects I could! found indexed item two *** -[NSCFArray objectAtIndex:]: index (3)
GS1 DataBar Truncated Printer In Java
Using Barcode creator for Java Control to generate, create GS1 DataBar Stacked image in Java applications.
www.OnBarcode.com
Denso QR Bar Code Reader In Visual Studio .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Crikey! That s one big index value. Stop the app, rerun in the debugger with Run Debug, wait for the halt, and see what we ve got:
Code 39 Extended Generator In None
Using Barcode printer for Microsoft Excel Control to generate, create Code-39 image in Microsoft Excel applications.
www.OnBarcode.com
PDF 417 Generation In None
Using Barcode printer for Online Control to generate, create PDF 417 image in Online applications.
www.OnBarcode.com
(gdb) po $eax *** -[NSCFArray objectAtIndex:]: index (2147483647( or possibly larger)) beyond bounds (3) (gdb) po [$eax name] NSRangeException
Draw USS Code 39 In Java
Using Barcode generation for Java Control to generate, create Code 3 of 9 image in Java applications.
www.OnBarcode.com
Barcode Drawer In None
Using Barcode maker for Office Excel Control to generate, create Barcode image in Office Excel applications.
www.OnBarcode.com
Now look at the call stack, and click on the uppermost item that s within our code: [ExceptionCityAppDelegate rangeException]. This will highlight the following line in the text editor:
Barcode Maker In None
Using Barcode creation for Online Control to generate, create Barcode image in Online applications.
www.OnBarcode.com
QR Creator In None
Using Barcode maker for Office Excel Control to generate, create QR image in Office Excel applications.
www.OnBarcode.com
NSLog(@"found indexed item %@", [array objectAtIndex:indexOfFive]);
This line actually makes two calls: first to the objectAtIndex: method, then to the NSLog function. A glance at the call stack shows that it s the objectAtIndex: method that is complaining. Apparently it doesn t like the value contained in indexOfFive. If you look in the variable view, or type p indexOfFive (note the use of p for standard C types, as opposed to po for Objective-C objects), you ll see 2147483647. That does seem a bit high! If you look at the code where it s set, just two lines earlier, you ll see this:
NSUInteger indexOfFive = [array indexOfObject:@"five"];
That line is asking array for the index of an object that it doesn t actually contain. In this case, NSArray returns a special integer value called NSNotFound, which is defined to be the maximum possible integer value. On Mac OS X running in 32-bit mode, that turns out to be 2147483647. This value is used to tell the caller that, Hey, that object you want the index of I don t have it. Which can be pretty useful to know! A consequence of this
CHAPTER 12: Exceptions, Signals, Errors, and Debugging
is that whenever you get a value from indexOfObject:, you really have to check to make sure that it s not NSNotFound. In our case we know exactly where the problem is, so we could just check that second time, but it s good to make a habit of always checking that return value, so we ll update the entire method like this:
- (void)rangeException { // assuming we have an array of things... NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; // ... we can ask for the index of an item... NSUInteger indexOfTwo = [array indexOfObject:@"two"]; if (indexOfTwo != NSNotFound) { // ... and we can later retrieve that value using the same index. NSLog(@"found indexed item %@", [array objectAtIndex:indexOfTwo]); } // But, what if we try to find the index for something that's not // there NSUInteger indexOfFive = [array indexOfObject:@"five"]; if (indexOfFive != NSNotFound) { // Good thing we check the return value to make sure it's not // NSNotFound! NSLog(@"found indexed item %@", [array objectAtIndex:indexOfFive]); } }
Make those changes, Build & Run, and you ll be rewarded with the congratulatory alert panel. Oh, sweet success! Having tackled those, you ve now experienced and fixed the main types of runtime exceptions that every Cocoa programmer gets at some point. That s it! This may come as a surprise to people come from other, more exception-heavy environments, but like we said, in Cocoa exceptions are used sparingly, and almost always to signal that the programmer has made an error. NSRangeException, and NSInvalidArgumentException (for both of the causes shown above) really make up the bulk of all exceptions you re likely to deal with.
Copyright © OnBarcode.com . All rights reserved.