Confused over try-catch

Discussion in 'iOS Programming' started by Danneman101, Aug 9, 2010.

  1. Danneman101 macrumors 6502

    Aug 14, 2008
    Im confused over how try-catch works in ObjC. In C#, for instance, when something goes bananas inside try-brackets the catch-brackets replaces that code assuring the program wont crash.

    In objc however, Im able to actually crash an app from within a try-bracket, like such:

    	@try {
    		for (int i=0; i < [self.strArr_Const_RowTitles count]; i++)
    			NSLog(@"TITLE: ");
    			NSLog([self.strArr_Const_RowTitles objectAtIndex:i]);
    	@catch (NSException * e) {}
    This code manages to crash the app at various instances. Im not as interested in whats wrong with the actual code inside the try-statement (sure, any ideas are welcome) as I am to the cause to why the execution of the code doesnt revert to the try and jumps to catch when it fails.
  2. seepel macrumors 6502


    Dec 22, 2009
    Try/Catch will only work if the code throws an exception. Without knowing more about your sample I can think of some things that will crash without an exception. For example trying to access memory that is deallocated won't throw an exception, it'll just crash.
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    try/catch works with NSExceptions aka run time exceptions. It doesn't catch hardware exceptions. Out of bounds, does not respond to selector, these are run time exceptions. Divide by zero, access a non valid address, these are hardware exceptions.

    Usually try/catch isn't too useful in iPhone apps. It's only useful in very specific kinds of code where a valid application might throw an exception. This involves things like NSFileHandle, NSScanner and a few others that throw exceptions for problems.
  4. Danneman101 thread starter macrumors 6502

    Aug 14, 2008
    Thanks for the excellent explanation. I've been under the impression that all errors should be handled by the try-catch-expression since in ObjC you seem to only have one exception - ie the NSException - whereas in for instance C# there are various types. Thanks for clearing that out :)
  5. ulbador macrumors 68000


    Feb 11, 2010
    You can make custom exceptions in objective C just like in many other C style languages. You make a class and extend the NSException class.

    The biggest different is that in other languages (Java comes to mind), everything throws an exception. In a language such as Java, an exception is used for practically everything and used as a method of diverting flow control.

    In Objective C, this is extremely resource intensive and less than ideal. Instead, you should be returning and manipulating error codes and NSErrors.

    Basically, the theory in Objective C is that exceptions should be used only for those cases that are truly unpredictable. Something like trying to put the letter "A" into a number type field is an expected and predictable condition and should be prevented or at least handled with error conditions instead of exceptions.
  6. pflau macrumors 6502

    Sep 17, 2007
    That is precisely why you are not getting what you expect to get from ObjC. C# is a MANAGED environment, which means that your program cannot possibly crash because the virtual machine always provides you with a way out.

    ObjC is really just an extension/superset of C. It runs on top of the hardware. It is up to you to manage your error handling. There is no safety net of a virtual environment.

Share This Page