我有很多麻煩,以異步DelegateCommand(棱鏡5)捕獲異常。異常處理棱鏡異步DelegateCommand
在視圖模型的構造:
cmdModificarPedido = New DelegateCommand(Of Object)(AddressOf OnModificarPedido, AddressOf CanModificarPedido)
在視圖模型:
Private _cmdModificarPedido As DelegateCommand(Of Object)
Public Property cmdModificarPedido As DelegateCommand(Of Object)
Get
Return _cmdModificarPedido
End Get
Private Set(value As DelegateCommand(Of Object))
SetProperty(_cmdModificarPedido, value)
End Set
End Property
Private Function CanModificarPedido(arg As Object) As Boolean
Return True
End Function
Private Async Sub OnModificarPedido(arg As Object)
Try
Await Task.Run(Sub()
Throw New Exception("My Exception")
End Sub)
NotificationRequest.Raise(New Notification() With {
.Title = "OK",
.Content = "Command OK"
})
Catch ex As Exception
NotificationRequest.Raise(New Notification() With {
.Title = "Error",
.Content = ex.Message
})
End Try
End Sub
爲什麼異常不被逮住?我知道這是另一個線程,但它應該由外部Try ... Catch塊(Try Catch outside of: await Task.Run(())捕捉。
注:這是vb.net代碼,但是C#的解決方案將被罰款了。
更新2 一旦我知道有我的調試器配置(第一次機會異常)的一個問題,我寫有同樣的問題更現實的例子:
Private Async Sub OnModificarPedido(arg As Object)
Try
Await Task.Run(Sub()
Try
throwMyException()
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub)
NotificationRequest.Raise(New Notification() With {
.Title = "Pedido Modificado",
.Content = "Pedido " + pedido.numero.ToString + " modificado correctamente"
})
Catch ex As Exception
NotificationRequest.Raise(New Notification() With {
.Title = "Error",
.Content = ex.Message
})
End Try
End Sub
Public Async Sub throwMyException()
Throw New Exception("My Exception")
End Sub
而且現在不抓住當我按F5時例外。它打破了「throwMyException()」,它不會捕獲異常,儘管它是在Try Catch塊中調用的。
重要提示:如果我刪除「throwMyException」中的異步詞,它確實會抓住它!
我現在做錯了什麼?
謝謝
您確定*此代碼*不會捕獲異常嗎? –
我絕對確定我生命中的東西很少:)但是我粘貼並複製了代碼,檢查了它,並沒有發現異常。我正在用圖片更新問題 –
看起來你在調試器中打開了第一次機會異常。如果你從那裏F5,它會被抓住? –