vb.net中的SQLexception

vb.net中的SQLexception,vb.net,Vb.net,我在SQL数据库中有两个表。一个表正在引用另一个表 我正在从VB.NET程序向表中添加数据。我想编写一个try…catch块来捕获在向表中添加数据时抛出的SQLException,其中可能会发生外键冲突。如何编写它?您可以尝试使用此代码 Try ... Catch ex As Exception MsgBox("Your exception" & ex.Message) End Try 链接:问题是关于VB.Net的,您编写的代码甚至都没有编译(在VB.Net和C中)。写t

我在SQL数据库中有两个表。一个表正在引用另一个表


我正在从VB.NET程序向表中添加数据。我想编写一个
try…catch
块来捕获在向表中添加数据时抛出的
SQLException
,其中可能会发生
外键
冲突。如何编写它?

您可以尝试使用此代码

Try
  ...

Catch ex As Exception
   MsgBox("Your exception" & ex.Message)
End Try

链接:

问题是关于VB.Net的,您编写的代码甚至都没有编译(在VB.Net和C中)。写
throw-ex
,应该是
throw
 Try
      'Do your stuff
 Catch ex as SqlException
      Dim errors = ex.Errors
      ' inspect errors to decide if this is the condition you want to catch
      Dim shouldCatch As Boolean = ... 
         ' Code that acts on the specific error condition
      If shouldCatch Then
      Else
         Throw ' rethrow everything we're not interested in
      End If
 End Try