我有一个有趣的问题,我正在捕捉异常(我在调试时遇到了“catch”断点)但是我的应用程序因以下代码中的未处理异常而崩溃:
HttpResponseMessage response = null;
try
{
response = await this.MyHttpClient.GetAsync( _url );
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
堆栈跟踪:
[0:] System.Net.WebException: The Internet connection appears to be offline. ---> Foundation.NSErrorException: Exception of type 'Foundation.NSErrorException' was thrown.
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <54e129cad3ee4c61a6b46c3680ddc69a>:0
at ModernHttpClient.NativeMessageHandler+<SendAsync>c__async0.MoveNext () [0x002f8] in <a827d263b32642a294603e46fea01afb>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <54e129cad3ee4c61a6b46c3680ddc69a>:0
at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x000a9] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <54e129cad3ee4c61a6b46c3680ddc69a>:0
缺少stackstrace是1行,它指的是包含“response = await this.MyHttpClient.GetAsync(_url);”
的类/行这怎么可能? NSErrorException继承自Exception,因此应该被捕获 - 也就是说,因为如果我在catch块中放置一个断点,则会触发断点。到底是怎么回事?我很困惑。
编辑:通过在平台(iOS)而不是在PCL中捕获异常,我设法得到这个不是非常有用的堆栈跟踪:
An unhandled exception occured.
2017-05-01 21:07:47.940 MyAppiOS[319:30171]
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
2017-05-01 21:07:47.940 MyAppiOS[319:30171] Unhandled managed exception:
Object reference not set to an instance of an object (System.NullReferenceException)
(null)
2017-05-01 21:07:47.941 MyAppiOS[319:30171] critical: Stacktrace:
2017-05-01 21:07:47.941 MyAppiOS[319:30171] critical:
Native stacktrace:
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 0 MyAppiOS 0x00000001000cd15c mono_handle_native_sigsegv + 256
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 1 libsystem_platform.dylib 0x000000018f539348 _sigtramp + 52
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 2 libsystem_pthread.dylib 0x000000018f53f450 pthread_kill + 112
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 3 libsystem_c.dylib 0x000000018f3eb400 abort + 140
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 4 MyAppiOS 0x00000001002012c0 xamarin_printf + 0
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 5 MyAppiOS 0x0000000100111814 mono_invoke_unhandled_exception_hook + 132
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 6 MyAppiOS 0x00000001000ccc60 mono_handle_exception_internal + 3784
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical: 7 MyAppiOS 0x00000001000cbd8c mono_handle_exception + 40
2017-05-01 21:07:48.138 MyAppiOS[319:30171] critical:
Failed to Stop app: An error occured on client IDB44034 while executing a reply for topic xvs/idb/4.4.0.34/stop-app
EDIT2:忽略网络异常,这是一个分心和我错误的假设(将打印的异常误认为是实际的堆栈跟踪,但它不是 - 它只是打印)。只有第二个堆栈跟踪是相关的。 但是,我确实解决了这个问题!的种类。如果我在Mac上使用Xamarin Studio运行我的应用程序,则没有问题。只有当我使用Visual Studio和构建主机运行我的应用程序时问题仍然存在。两者都运行相同的,最新的,xamarin和mac os。非常奇怪......我确实设法找到了这个报告的错误:https://bugzilla.xamarin.com/show_bug.cgi?id=17419但是使用建议的解决方案无法取得成功。我现在只在Mac上运行我的应用程序。