Amazon.com Widgets Chapter 9, Exercise 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 08:17:44 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 9
| | | |-+  Chapter 9, Exercise 6
Pages: [1]   Go Down
Print
Author Topic: Chapter 9, Exercise 6  (Read 1098 times)
nate
Newbie
*
Posts: 9






« on: February 19, 2009, 10:20:54 AM »

I can't seem to get a division by zero exception to be trapped by a simple try/catch block.  I tried the following code:

Code: (Objective-C)
    @try {
        accumulator /= value;
    }
    @catch ( NSException* exception ) {
        NSLog( @"Caught %@: %@", [exception name], [exception reason] );
    }

Which didn't do much (the division by zero produced -inf, but no exception was raised). 

So I changed the program to use integers instead of floating point numbers, after which the program would exit prematurely with the message "Floating point exception."  Which is odd for two reasons: 1) it is all integer math now, so why a floating point exception and 2) why wasn't the exception propagated up from the system into the Objective-C try/catch block?

Ultimately, I read the Xcode documentation section on throwing exceptions and found an example of how to throw one, so I solved the Exercise as follows (which works for both floating point and integer math):

Code: (Objective-C)
    @try {
        if ( value == 0 ) {
            NSException *e = [NSException exceptionWithName: @"Floating point exception"
                                          reason: @"Division by zero" userInfo: nil];
            @throw e;
        }
        accumulator /= value;
    }
    @catch ( NSException* exception ) {
        NSLog( @"Caught %@: %@", [exception name], [exception reason] );
    }


Perhaps I've missed something and this is indeed the intended solution for the exercise? 

If not, is there something I'm doing wrong?  It sure seems a lot more convenient to write the simpler try/catch and count on the system exception climbing up the exception chain instead of using the manual check in my solution.

Logged
mdeh
Full Member
***
Posts: 166






« Reply #1 on: February 19, 2009, 11:18:27 AM »

Hi Nate,
Not to worry...that exercise is deleted. Go to the errata and download the errata for the 2nd printing. I was trying to find the discussion about this, but that seems to have gone too...but it , as you have found it, is not possible to do given the current OS and the exercise as written.

Here's a reference:

http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/universal_binary_diffs/chapter_3_section_9.html
« Last Edit: February 19, 2009, 07:36:58 PM by mdeh » Logged
nate
Newbie
*
Posts: 9






« Reply #2 on: February 19, 2009, 12:42:50 PM »

Aha!  I should have thought to check out the errata!  Thanks for the pointers to the errata and reference link.

Logged
mdeh
Full Member
***
Posts: 166






« Reply #3 on: February 19, 2009, 02:03:18 PM »

Aha!  I should have thought to check out the errata!  Thanks for the pointers to the errata and reference link.



You are most welcome

Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.