2012-02-29 31 views
7

Trong MonoTouch, làm thế nào để đăng ký một handler còn tự do ngoại lệ (hoặc chức năng tương tự)MonoTouch: uncaughtExceptionHandler?

Trong obj-C:

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

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

Trả lời

-4

Bạn thêm một handler try-catch xung quanh mã đó (có thể) ném NSExceptions :

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

MonoTouch đã cài đặt trình xử lý ngoại lệ chưa xử lý và tự động dịch các ngoại lệ ObjectiveC này sang ngoại lệ được quản lý.

+2

Điều này thực sự không giải quyết được câu hỏi ban đầu của anh ấy. –

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

Tuyệt vời, cảm ơn bạn! Chỉ có điều là hàm khởi tạo cho NSException mà lấy một IntPtr được bảo vệ, vì vậy bạn cần phải loại bỏ nó và hiển thị phiên bản của hàm tạo đó. Nếu không thì điều này rất hữu ích. –

+0

[Ở đây] (http://stackoverflow.com/questions/37525472/create-nsexception-from-intptr/37527174#37527174) bạn có thể thấy cách 'NSException' chấp nhận một' IntPtr' làm đối số. – testing

0

Điều này thực hiện công việc. Gọi phương thức SetupExceptionHandling() khi khởi động ứng dụng. Phép thuật là phần NSRunLoop. Nhưng ứng dụng sẽ ở trạng thái kỳ lạ vào thời điểm đó, với các hiệu ứng không thể đoán trước. Vì vậy, tôi mạnh mẽ sẽ đề nghị giết chết ứng dụng sau khi người dùng quyết định phải làm gì với ngoại lệ - ví dụ, bằng cách ném lại nó.

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); 
    } 
}