views:

85

answers:

1

Hi all,

I am writing a simple iPhone application and I am wondering if there is something equivalent to C#'s try{}-catch{}-finally{} exception handling.

I have come across a few references via google, but I am having trouble figuring out what the actual syntax is on the iPhone platform.

Does anyone know of a very basic example on how to do this?

Many thanks, Brett

+1  A: 

The actual syntax on the iPhone platform/framework is the same as it is in obj-c because it is still obj-c with a set of classes. This kinda stuff is easily found in the online obj-c documentation provided by apple and any intro obj-c book. The Google-fu answer:

Cup *cup = [[Cup alloc] init]; 

@try { 
    [cup fill]; 
} 
@catch (NSException *exception) { 
    NSLog(@"main: Caught %@: %@", [exception name], [exception reason]); 
} 
@finally { 
    [cup release]; 
} 

Now, if you are looking for how this code fits into an iPhone application then you can download a sample application and copy the syntactical structure.

P.Brian.Mackey
Thats exactly what I needed, thanks! I hadn't found an answer as simple as that (which is what I was going for).
Brett
No problemo! GL
P.Brian.Mackey
Again -- do *not* use exceptions for recoverable error indication when targeting the iPhone or Mac OS X. Exceptions should only be used for non-recoverable errors.
bbum