Skip to main content

Posts

Showing posts with the label error-handling

Try-catch exception handling practice for iPhone/Objective-C

Apologies if this question has already been answered somewhere else, but I could not find any decisive answer when searching on it: I'm wondering when try-catch blocks are to be used in objective-c iPhone applications. Apple's "Introduction to the Objective-C Programming Language" state that exceptions are resource intensive and that one should "not use exceptions for general flow-control, or simply to signify errors." From reading a few related questions here I also gather that people are not often using this method in practice. So I guess the question is: what are the situations when it's appropriate to use try-catch blocks when developing for iPhone/Objective-C and when should they absolutely NOT be used? For example, I could use them to catch beyond bounds and other exceptions when working with objects in arrays. I have a method which performs are few tasks with objects that are passed on in a number of arrays. The method returns nil if an er

iPhone - How to handle errors at runtime

When writing code, there are many situations that must be treated as runtime errors : an alloc/init returns nil, a resource is not found, a [someClass canDoThis] returns NO for an absolutely-needed feature where YES would be the natural answer, ... For all these situations, I have written an exitWithMessage routine (that displays an alert box), and each class has a kill method that frees allocated memory. So... When in an init method, you have these kind of exceptions, I supposed you could do : [self kill]; [OneClass exitWithFatalErrorMessage]; return nil; - (void) exitWithFatalErrorMessage:(NSString*)message { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedStringFromTable(@"Error" @"ErrorMessages", @"") message:message delegate:self cancelButtonTitle:NSLocalizedStringFromTable(@"Understood", @"ErrorMessages", @"") otherButtonTitles: nil]; [alert show]; [alert release]; } - (void)alertV

iPhone - How to test allocated objects returned by each memory call?

When writing an app, you always have to write alloc/inits, get autoreleased datas returned by the framework classes, ... This may be 70% of the code, almost each single line of what you write... So... How do those returned object must be tested, to know if each of these calls have returned a correct object ? Testing the returned value each time, for each call, and treating the exception if you get nil where you expected an allocated object ? Letting the app crash ? How this must be done ?

Distinguish new vs existing exceptions

I am creating a PHP application and I want to display the number of times an error has occurred. The problem that I am trying to figure out is if an error has already been reported or if its new using the following values: Message (ie: Attempted to divide by zero.) Stack trace (ie: at componentNETapp.Form1.btnTrackExceptionsUn_Click(Object sender, EventArgs e) ...) Source (ie: componentNETapp) Target site (ie: Void btnTrackExceptionsUn_Click(System.Object, System.EventArgs)) Thanks