2

Is there anyway in ios to handle all the exceptions that occurred in entire application in a single try catch block.

If yes where should I write this block.

AshokGK
  • 875
  • 1
  • 10
  • 20
  • 2
    Unfortunately, no. You have to create code which will work well:) You can analyze crash log for looking for all critical exeptions to fix their. The are useful services for getting crashes(e.g Crashlytics). – gaRik Apr 16 '15 at 10:54
  • I would also ask yourself "why are you handling unhandled exceptions?" The better solution might be to make sure of an object and its state before trying to use that object. I am not a fan of Try/Catch unless you are dealing with some third party library of which you have no control. For things you do have control of (your code), Try/Catch should be questioned whenever used. – LJ Wilson Apr 16 '15 at 11:28

3 Answers3

0

Assuming the question is about Objective-C there is no need to handle exceptions because they are not to be used for program control. Exceptions are only to be used to catch non-recoverable programming errors so if you get one the code needs to be fixed.

In general exceptions across stack frames are not handled in a manner that is recoverable.

The best thing to do is add an exception breakpoint in Xcode so you can better examine exceptions:

From the Mian Menu Debug:Breakpoints:Create Exception Breakpoint. Run the app to get the breakpoint. When you hit the exception breakpoint click debug continue a couple of times and you will get a backtrace and more error info. Add that and an exact copy of Xcode/Debugger messages to the question.

It is also a good idea to change this breakpoint to "Objective-C only."

zaph
  • 111,848
  • 21
  • 189
  • 228
0

You might want to have a look into the concept of handling exceptions globally in iOS. I might not be able to give the most correct answer but these links can spread some more light on what you might just want.

iOS Global Exception Handling

Global Exception Handler in iOS

Hope this helps.

Community
  • 1
  • 1
Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • In Objetive-C exceptions can not reliably be recovered from and can not successfully be used for program control. They are only to be used to catch non-recoverable programming errors. – zaph Apr 16 '15 at 10:50
  • Of Course yes, they provide more complex stack traces and messages, but if you need a top level Exception handler, there might not be any other options available, excuding what OP asked, because that is something not available into iOS scope. – Dhrumil Apr 16 '15 at 10:55
  • Objective-C does not clean-up the objects that are on the stack on exceptions, this includes strongly retained objects. – zaph Apr 16 '15 at 11:00
0

You can use NSSetUncaughtExceptionHandler to set the handler.

Swift:

NSSetUncaughtExceptionHandler { (exception) in
    while true {
        sleep(UInt32(0.5))
    }
}

Objective-C:

void handleException(NSException* exception) {
    while true {
        sleep(0.5);
    }
}

NSSetUncaughtExceptionHandler(handleException);

When the handler is returned, the app crashes and Xcode shows the error. So you can run a loop until you want to quit the app. I do not recommend to keep the app running normally, but you can show for example an alert saying an error occurred and when the user taps a button, the app crashes. Use a flag for the loop.

Emma Labbé
  • 667
  • 7
  • 18