2012-02-29 14 views
7
MonoTouch yılında

, ben yakalanmamış istisna işleyicisi (veya benzeri işlevi) Obj-CMonoTouch: uncaughtExceptionHandler?

kayıt nasıl:

void uncaughtExceptionHandler(NSException *exception) { 
     [FlurryAnalytics logError:@"Uncaught" message:@"Crash!" exception:exception]; 
    } 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 
    [FlurryAnalytics startSession:@" "]; 
    .... 
} 

cevap

-4

Sen NSExceptions atmak (belki) bu kodu etrafında bir try-catch işleyici ekleyin :

try { 
    FlurryAnalytics.StartSession (" "); 
} catch (MonoTouchException ex) { 
    Console.WriteLine ("Could not start flurry analytics: {0}", ex.Message); 
} 

MonoTouch zaten yakalanmamış istisna işleyicisi yükler ve otomatik olarak yönetilen istisnalara bu ObjectiveC istisnalar çevirir.

+2

Bu gerçekten onun özgün sorusunu sormaz. –

1
public delegate void NSUncaughtExceptionHandler(IntPtr exception); 

    [DllImport("/System/Library/Frameworks/Foundation.framework/Foundation")] 
    private static extern void NSSetUncaughtExceptionHandler(IntPtr handler); 

    // This is the main entry point of the application. 
    private static void Main(string[] args) 
    { 
      NSSetUncaughtExceptionHandler(
       Marshal.GetFunctionPointerForDelegate(new NSUncaughtExceptionHandler(MyUncaughtExceptionHandler))); 

      ... 
    } 

    [MonoPInvokeCallback(typeof(NSUncaughtExceptionHandler))] 
    private static void MyUncaughtExceptionHandler(IntPtr exception) 
    { 
     var e = new NSException(exception); 
     ... 
    } 
+0

Harika, teşekkürler! NSException için bir IntPtr korumalı olan kurucunun tek şey, bu yüzden alt türünü yazmanız ve kurucunun bu sürümünü açığa çıkarmanız gerekir. Aksi halde bu süper yararlı oldu. –

+0

[Burada] (http://stackoverflow.com/questions/37525472/create-nsexception-from-intptr/37527174#37527174) 'NSException' argüman olarak bir 'IntPtr' nasıl kabul ettiğini görebilirsiniz. – testing

0

Bu işi yapar. Uygulama başlatıldığında SetupExceptionHandling() yöntemini çağırın. Sihir NSRunLoop parçasıdır. Ama uygulama, öngörülemeyen etkileri ile, bu noktada tuhaf bir durumda olacak. Bu nedenle, kullanıcı istisna ile ne yapacağına karar verdikten sonra uygulamayı öldürmeyi öneririm - örneğin, yeniden atarak.

public static class IOSStartupTasks { 
    private static bool _HaveHandledException; 
    public static void HandleException(object sender, UnhandledExceptionEventArgs e) { 
    if (!(_HaveHandledException)) { 
     _HaveHandledException = true; 
     UIAlertView alert = new UIAlertView("Error", "Bad news", "report", "just crash"); 
     alert.Delegate = whatever; // delegate object should take the exception as an argument and rethrow when it's done handling user input. 
     alert.Show(); 
     NSRunLoop.Current.RunUntil(NSDate.DistantFuture); // keeps the app alive, but likely with weird effects, so make sure you don't let the user back into the main app. 
    } 
    } 

    public static void SetupExceptionHandling() { 
    AppDomain domain = AppDomain.CurrentDomain; 
    domain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => 
     IOSStartupTasks.HandleException(sender, e); 
    } 
} 
İlgili konular