Catch NSException c#xamarin

时间:2016-07-05 07:54:24

标签: c# ios xamarin core-bluetooth

我一直在尝试捕获NSException抛出的CancelPeripheralConnection,但它一直说它需要从system.exception派生。

try
{
     if (device.NativeDevice as CBPeripheral != null)
     {
          this.central.CancelPeripheralConnection(device.NativeDevice as CBPeripheral);
     }
     else
     {
          System.Diagnostics.Debug.WriteLine("Null CBPeripheral Disconnect");
     }
}
catch (Foundation.NSErrorException)
{
    //works
    System.Diagnostics.Debug.WriteLine("Caught ns Error exception");
}
catch (Foundation.NSException)
{
    //doesn't work
    System.Diagnostics.Debug.WriteLine("Caught ns exception");
}

当崩溃 2016-07-05 08:41:16.579 BadgeManageriOS[1066:685789] *** Assertion failure in -[CBCentralManager cancelPeripheralConnection:force:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreBluetooth/CoreBluetooth-327.3/CBCentralManager.m:332

时,我在输出窗口中不断收到以下信息

我的问题是我应该如何抓住NSException,因为我似乎无法找到人们在它抛出它的地方捕获它的任何文档。

2 个答案:

答案 0 :(得分:0)

ObjC NSException有一个C#托管包装类:MonoTouchException

MonoTouchException thrownException = null;
try {
  ~~~~
} catch (MonoTouchException ex) {
    thrownException = ex;
}

https://developer.xamarin.com/api/type/MonoTouch.Foundation.MonoTouchException/

  

这是Objective-C NSException类的包装类。

答案 1 :(得分:0)

我认为NSException是你不应该自己处理的事情。这是本机抛出的。查看:https://bugzilla.xamarin.com/show_bug.cgi?id=18427