Tuesday, January 3, 2017

Throwing an exception from anywhere in project

Leave a Comment

I want to show alert whenever an exception occured anywhere in my code.

Without enclosing the code that may throw exception.

Everytime when exception occures it shows error in main class. Is that possible to handle from there?

This is normal way of throwing an exception im using:

@try {  // code that may throw exception   }  @catch (NSException * e) {   // show alert  }  @finally {       } 

4 Answers

Answers 1

However, I have found an effective work-around - creating my own exception handler (which is also useful for other reasons). First, create a function that will handle the error and output it to the console (as well as whatever else you want to do with it)

    void uncaughtExceptionHandler(NSException *exception) {       NSLog(@"CRASH: %@", exception);       NSLog(@"Stack Trace: %@", [exception callStackSymbols]);       // Internal error reporting     } 

Next, add the exception handler to your app delegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions      {            NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);          // Normal launch stuff      } 

If this doesn't work, then there are only two possible reasons:

Something is overwriting your NSSetUncaughtExceptionHandler call (there can be only one handler for your entire app). For example, some 3rd party libraries set their own uncaughtExceptionHandler. So, try setting it at the END of your didFinishLaunchingWithOptions function (or selectively disabling 3rd party libraries). Or better yet, set a symbolic break point on NSSetUncaughtExceptionHandler to quickly see who is calling it. What you may want to do is to modify your current one rather than adding another one. You're not actually encountering an exception (for example, EXC_BAD_ACCESS is not an exception)

Answers 2

Navigate to Breakpoint panel in Xcode, click "plus" button at the left-bottom to add a new breakpoint, then do this:

enter image description here

build and run your project, Xcode will brings you to the line where exception throws.

Answers 3

I have added exception handling code inside my main.m.

 int main(int argc, char * argv[]) {             @try {                 @autoreleasepool {                  return UIApplicationMain(argc, argv, nil, NSStringFrom  Class([AppDelegate class]));             }          } @catch (NSException *exception) {              NSLog(@"exception");         }     } 

Its working now.

Answers 4

The best answer I can suggest is not to do this. In Objective-C exceptions are generally reserved for programmer errors and it's generally best to crash when they're hit.

Direct from the source, Apple strongly suggests that you not use exceptions for error handling.

You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server. You usually take care of these sorts of errors with exceptions when an application is being created rather than at runtime.

Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications.

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Exceptions/Exceptions.html#//apple_ref/doc/uid/10000012i

Although exceptions are commonly used in many programming environments to control programming flow or to signify errors, do not use exceptions in this way in Cocoa and Cocoa Touch applications. Instead, you should use the return value of a method or function to indicate that an error has occurred, and provide information about the problem in an error object.

https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/ExceptionHandling.html#//apple_ref/doc/uid/TP40008195-CH18-SW1

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment