.net-在行变更事件中捕获异常

时间:2019-03-03 18:15:20

标签: events datatable try-catch

我有一堂课,上面有许多针对数据表的操作。此类中的表触发事件。不能从调用方获取事件处理程序中的异常。

这里有一个样本表格:

    Public Class Form1

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim TbAction As New cTbAction
    Call TbAction.DoAnyAction()
  End Sub
End Class


Public Class cTbAction
  Public WithEvents Tb As DataTable
  Public Sub New()
    Tb = New DataTable
    Tb.Columns.Add(New DataColumn("Text", GetType(String)))
  End Sub

  Private Sub Tb_RowChanged(sender As Object, e As DataRowChangeEventArgs) Handles Tb.RowChanged
    Throw New Exception("Test in RowChanged")
  End Sub

  Public Sub DoAnyAction()
    Try
      Dim Dr = Tb.NewRow
      Dr(0) = "huhu"
      Tb.Rows.Add(Dr)
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try
  End Sub
End Class

我在做什么错?致以最诚挚的谢意。

0 个答案:

没有答案