Search code examples
c#iosxamarinxamarin-studio

Strange exceptions stack trace in Xamarin Studio


enter image description here Get next strange Xamarin Studio behaviour: I throw hard-coded exception in specific line of some class.

enter image description here Result is next: catch exception always in Main class without my specific line in stack trace


Solution

  • The method OpenRelatedView() seems to be called from a non-UI thread because you are using InvokeOnMainThread() in there. So my assumption is you are using Task.Run() somewhere to call it. All async code will implicitly be wrapped into a try-catch.

    This exception must be caught and processed by you. Every Task object has got an Exception property that will contain all the exceptions that happened during the async execution. If you are using await correctly, these exceptions will be unwrapped. Your method has a void return type so it cannot be awaited. Hence the exception will remain undetected until something takes care of it. On some platforms (windows Phone for instance) you will never notice that something went wrong.

    With Xamarin.iOS however, there is handler for all uncaught exceptions at app root level, that's why you'll see the exception there.

    Steven Clery has a lot of good articles about tasks ans async coding.