ConstraintException 未被捕获

ConstraintException Not Being Caught

第一次海报。我在这里享受了多年的帮助。谢谢大家。

我遇到了一个看起来不应该发生的情况。

在 VS2017 社区中使用 VB.NET,我在一个 Try 块中得到一个 System.Data.ConstraintException,我在其中专门捕获了那个确切的异常。

消息如下所示:

System.Data.ConstraintException: '列 'PAIR1, PAIR2, PAIR3' 被限制为唯一。值 'CHATBTC, ETHBTC, CHATETH' 已经存在。'

https://www.dropbox.com/s/d91rgtwsjwioqhm/SO_error.jpg?dl=0

正如您从逻辑上可以看出的那样,我指望触发异常,以便我可以构建一个 table 唯一行并添加到我的重复行值中。随着 table 大小的增长,在 ADD 之前检查重复项会花费大量时间,因此这种方法是最快的。

不是每次都这样,只有30%左右。我的应用程序还远远不够 运行 在生产中,所以我看到的一切都是在调试时。

我的代码在这里:

  tblTriArbPairs.PrimaryKey = New DataColumn() {tblTriArbPairs.Columns("PAIR1"), tblTriArbPairs.Columns("PAIR2"), tblTriArbPairs.Columns("PAIR3")}

  Try
      tblTriArbPairs.Rows.Add(
    Pairs(0), Pairs(1), Pairs(2),
    idxPair0, idxPair1, idxPair2,
    result.TD1, result.TD2, result.TD3,
    CoinOnly(Pairs(0)), CurrOnly(Pairs(0)),
    CoinOnly(Pairs(1)), CurrOnly(Pairs(1)),
    CoinOnly(Pairs(2)), CurrOnly(Pairs(2)),
    FindLoopCoin(CoinOnly(Pairs(0)), CurrOnly(Pairs(0)), CoinOnly(Pairs(1)), CurrOnly(Pairs(1)), CoinOnly(Pairs(2)), CurrOnly(Pairs(2))),
    GetSymbolLIQ(Pairs(0)), GetSymbolLIQ(Pairs(1)), GetSymbolLIQ(Pairs(2))
    )
      RowsAdded += 1
  Catch ex As System.Data.ConstraintException
      DupRows += 1
  Catch ex As Exception
  Finally
  End Try

填充 table 后,我最终添加了 3480 行和 2640 行重复项。错误发生的时间不一致。有时马上,有时快到最后。

我查看了所有内容,但没有发现任何解决未捕获 ConstraintException 的问题。其他例外,是的。

非常感谢任何帮助。希望我提出了一个好问题。 :)

我读到捕获异常是一种相当麻烦的引导程序流程的方法,最好防止异常。

我认为这是您要处理的数据表,因此下面的 Linq 代码可能可以解决问题。根据文档 "The enumeration of source is stopped as soon as the result can be determined."

我刚刚使用示例数据库进行了测试。

    Dim CoffeeName As String = "Black Tiger"
    Dim CoffeeType = "Decaf"

    Dim dup As Boolean = dt.AsEnumerable().Any(Function(Row) CoffeeName = Row.Field(Of String)("Name") And CoffeeType = Row.Field(Of String)("Type"))
    MessageBox.Show(dup.ToString)

编辑 使用您的变量和 3 个字段编写的代码。

    Dim dup As Boolean = tblTriArbPairs.AsEnumerable().Any(Function(Row) Pairs(0) = Row.Field(Of String)("Pair1") _
        And Pairs(1) = Row.Field(Of String)("Pair2") And Pairs(2) = Row.Field(Of String)("Pair3"))
    If dup Then
        DupRows += 1
        MessageBox.Show("Sorry, duplicate")
        Exit Sub
    End If
    'Add the row

我从来没有解决过为什么抛出异常的问题,尽管我明确地陷入了它。我确实听从了这里的建议,在尝试将它们添加到我的数据表之前检查是否存在重复项。这是我想出的,它似乎工作得很好。

Dim iRow As DataRow() = tblTriArbPairs.Select("PAIR1 = '" & Pairs(0) & "' AND PAIR2 = '" & Pairs(1) & "' AND PAIR3 = '" & Pairs(2) & "'")
If iRow.Count = 0 Then
    tblTriArbPairs.Rows.Add(
      Pairs(0), Pairs(1), Pairs(2),
      idxPair0, idxPair1, idxPair2,
      result.TD1, result.TD2, result.TD3,
      CoinOnly(Pairs(0)), CurrOnly(Pairs(0)),
      CoinOnly(Pairs(1)), CurrOnly(Pairs(1)),
      CoinOnly(Pairs(2)), CurrOnly(Pairs(2)),
      FindLoopCoin(CoinOnly(Pairs(0)), CurrOnly(Pairs(0)), CoinOnly(Pairs(1)), CurrOnly(Pairs(1)), CoinOnly(Pairs(2)), CurrOnly(Pairs(2))),
      GetSymbolLIQ(Pairs(0)), GetSymbolLIQ(Pairs(1)), GetSymbolLIQ(Pairs(2))
      )
    RowsAdded += 1
Else
    DupRows += 1
End If

再次感谢所有提供帮助的人。我通过了我的第一个 SO 问题!是啊!