locked
Problem catching an exception in Async function

    Question

  • Hi,

    I have the below async code which I am trying to catch in an exception when the method runUpload1 (called in UploadProcess1 function) falls over.  I have tried moving the try/catch around and have tried all the examples from stackoverflow using 'await' and the -

     Catch ex As ProtocolException
        End Try

    But nothing will work for me...is there something obvious I am missing. 

     Private Sub Upload_Tapped(sender As Object, e As TappedRoutedEventArgs)
            Try
                tbResults.Text = ""
                Dim task As New Task(New Action(AddressOf UploadProcess1))
                task.Start()
    
                getToast("Upload1 Complete", "All data transferred")
    
            Catch ex As Exception
                getToast("ERROR", "Contact Support - " & ex.Message & "")
                tbResults.Text = "ERROR - " & ex.Message & ""
    
            End Try
    
        End Sub

     Private Async Function UploadProcess1() As Task
    
            Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, Function()
                                                                                         ShowStatus("Start of Upload...")
                                                                                     End Function)
            Dim upload1 As String = Await runUpload1()
            Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, Function()
                                                                                         ShowStatus(upload1)
                                                                                     End Function)
    
            Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, Function()
                                                                                         ShowStatus("End of Upload...")
                                                                                     End Function)
    
    
        End Function

    Friday, October 24, 2014 1:35 PM

Answers

All replies

  • Await your UploadProcess call should help.

    As is, the function and exception handler will exit before UploadProcess finishes

    Running under the debugger to see the first chance exceptions may help narrow things down.

    Give http://msdn.microsoft.com/en-us/magazine/jj991977.aspx a read.

    If that doesn't help please share a minimal sample that demonstrates the problem with clear repro steps.

    Friday, October 24, 2014 2:03 PM
    Owner
  • Thanks you pointed me in the right direction...I changed the below to 'await UploadProcess1' and the catch now works.

      Dim task As New Task(New Action(AddressOf UploadProcess1))
                task
    .Start()

    Friday, October 24, 2014 10:57 PM