Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement global iPhone Exception Handling?

I have one crash in my iPhone application that does throw an NSException. The crash reports are completely ambiguous in where the error is and what exactly is causing it. Is there a smart way for me to set a top level exception handler somewhere to see what is causing it? I can't replicate the problem myself, but a few of my beta users certainly can.

What's a smart way to handle a problem of this nature?

like image 408
Coocoo4Cocoa Avatar asked Aug 15 '09 17:08

Coocoo4Cocoa


People also ask

How do you handle global exception?

In the Design tab part of the Ribbon, select New > Global Handler. The New Global Handler window opens. Type in a Name for the handler and save it in the project path. Click Create, a Global Exception Handler is added to the automation project.

How we can handle global exception in spring boot?

The Controller Advice class to handle the exception globally is given below. We can define any Exception Handler methods in this class file. The Product Service API controller file is given below to update the Product. If the Product is not found, then it throws the ProductNotFoundException class.

What is IOS exception?

In short, exceptions cause applications to crash if left unhandled. They generally occur when trying to perform an operation on an object incorrectly, such as using an out-of-bounds index to access an array item, or passing nil to a method that doesn't accept it. In other words, they are caused by developer mistakes.

Can we implement exception handling?

Example: Exception handling using try...To handle the exception, we have put the code, 5 / 0 inside the try block. Now when an exception occurs, the rest of the code inside the try block is skipped. The catch block catches the exception and statements inside the catch block is executed.


1 Answers

It seems like you are asking two questions here: how to set a top level exception handler; and how to deal with the issue of determining what the root cause is.

Catching the exception can be done in a few different ways, but for this the best approach would appear to be to set an exception handler using NSSetUncaughtExceptionHandler.

When an exception is raised in your app, it is handled by a default exception handler. This handler does nothing more than log a message to the console before the app closes. You can over-ride this by setting you own custom exception handler using the function stated above. The best place to do this would be in the app delegate applicationDidFinishLaunching: method.

- (void)applicationDidFinishLaunching:(UIApplication *)application {     NSSetUncaughtExceptionHandler(&myExceptionHandler); } 

Once you've set a custom handler, you'll want to expand on the default output in helping you determine what the cause is.

void myExceptionHandler(NSException *exception) {     NSArray *stack = [exception callStackReturnAddresses];     NSLog(@"Stack trace: %@", stack); } 

Unfortunately compared to OSX the iPhone appears quite limited in respect to producing a nice stack trace. The code above will produce some seemingly junk output; however, you can run this output through the atos tool, and you should be able to generate a useful stack trace from it.

Another option is to follow the instructions on this article which will help to produce a nice stack trace automatically.

As this is going out to beta testers you may have to tinker about to get it working for you.

You say that you've not been able to replicate the problem yourself, only your users. In this case you may find this technical note from Apple useful:

https://developer.apple.com/library/content/technotes/tn2151/_index.html

UPDATE: While this post still contains useful info, some of the links it contains are dead irreversibly. It is advised to use the info from this alternative post.

like image 54
Paul McCabe Avatar answered Oct 13 '22 06:10

Paul McCabe