2

This is catch the exception code

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface NdUncaughtExceptionHandler : NSObject {
}
+ (void)setDefaultHandler;
@end

-

#import "UncaughtExceptionHandler.h"

void UncaughtExceptionHandler(NSException *exception) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
}

@implementation NdUncaughtExceptionHandler

+ (void)setDefaultHandler {
    NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler); 
}
@end

Call the method is NdUncaughtExceptionHandler.setDefaultHandler(), but does not work in the swift.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        NdUncaughtExceptionHandler.setDefaultHandler()
}

It can work in the Objecttive-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NdUncaughtExceptionHandler setDefaultHandler];
}

What went wrong?

Injoy
  • 174
  • 1
  • 11
  • Can you describe *"does not work in Swift"* more precisely? Does it not compile, does it crash, does it not give the expected results? – Martin R Jun 24 '15 at 09:30
  • My English is not good. It can be compiled and run, but can not catch the exception. But with OC, it will open the page after the application crash – Injoy Jun 24 '15 at 09:50
  • What exception? How did you test it? – Martin R Jun 24 '15 at 09:51
  • Trigger an error, like: `var a = [""]; println(a[2]);` – Injoy Jun 24 '15 at 09:56
  • is your crash happening at: NdUncaughtExceptionHandler.setDefaultHandler() or is it happening when you have a NSSet Uncaught Exception somewhere else in your code? – Mikael Jun 24 '15 at 11:12
  • no。This is my new discovery http://stackoverflow.com/questions/31024803/swift-how-to-deal-with-uncaught-exception – Injoy Jun 25 '15 at 02:50

0 Answers0