PDA

View Full Version : Confused over try-catch




Danneman101
Aug 9, 2010, 04:47 PM
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.



seepel
Aug 9, 2010, 07:35 PM
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.

PhoneyDeveloper
Aug 9, 2010, 09:34 PM
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.

Danneman101
Aug 10, 2010, 12:45 PM
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 :)

ulbador
Aug 10, 2010, 02:25 PM
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 :)

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.

pflau
Aug 10, 2010, 02:50 PM
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.


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.