我有主窗口表单,我有用户控制。从那里我做了一些与数据库相关的操作。在数据库过程中,我向用户显示其他窗口形式,以便在此期间可以看到漂亮的圆形图形。我使用任务来放置提到的数据库进程,如果发生错误,我想从那里显示来自try / catch的错误消息。不幸的是MessageBox.Show在我做测试时没有显示 - 甚至没有达到捕获。你能帮帮我解决问题吗?我正在谈论这一行中的捕获量:
Catch sqlex As Exception
pic.Invoke(Sub() MessageBox.Show(pic, sqlex.Message))
此处的循环窗口的表单代码:
Public Class FrmCircularProgress
Sub New(progressType As DevComponents.DotNetBar.eCircularProgressType)
InitializeComponent()
CircularProgress1.ProgressBarType = progressType
StartCircular()
End Sub
Public Sub StartCircular()
Me.CircularProgress1.IsRunning = True
End Sub
Public Sub StopCircular()
Me.CircularProgress1.IsRunning = False
End Sub
End Class
这是我如何在任务中使用它的示例:
Dim createArticle As New Artikel
Dim pic As New FrmCircularProgress(eCircularProgressType.Donut)
Dim tsk As Task(Of Boolean) = Task.Factory.StartNew(Of Boolean)(Function()
'--Run lenghty task
Dim resu = False
Try
resu = createArticle.ProcessArticle(_artikelsAndTheirVariationsFinal)
'--Close form once done (on GUI thread)
Catch sqlex As Exception
pic.Invoke(Sub() MessageBox.Show(pic, sqlex.Message))
Finally
pic.Invoke(New Action(Sub() pic.StopCircular()))
pic.Invoke(New Action(Sub() pic.Close()))
End Try
Return resu
End Function)
'--Show the form
pic.ShowDialog()
Task.WaitAll(tsk)
...
只是为了让你在我们的案例ProcessArticle中看到示例数据库进程,它返回true或false
Public Function ProcessArticle(artikel As ArticlesVariations) As Boolean
Dim result = True
Dim strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()
Using connection As New SqlConnection(strcon)
'-- Open generall connection for all the queries
connection.Open()
'-- Make the transaction.
Dim transaction As SqlTransaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
Dim newArticleRowId As Integer = 0
Dim articleIndex As Integer = 0
Try
For Each kvp As KeyValuePair(Of Integer, Artikel) In artikel.collection
Dim ckey As Integer = kvp.Key
articleIndex = kvp.Key 'save article key
Dim data As Artikel = kvp.Value
'-- If given article contains images list (artikel_images is a list with pictures associated with article)
If Not IsNothing(artikel.collection(articleIndex).ArtikelImages) Then
For Each img In artikel.collection(articleIndex).ArtikelImages
'--Insert article's images if exists
Using cmd As New SqlCommand("INSERT INTO T_Article_Image (Path, FK_Artikel_ID, Position) VALUES (@Path, @FK_Artikel_ID, @Position)", connection)
cmd.CommandType = CommandType.Text
cmd.Connection = connection
cmd.Transaction = transaction
cmd.Parameters.AddWithValue("@Path", img.Path)
cmd.Parameters.AddWithValue("@FK_Artikel_ID", newArticleRowId)
cmd.Parameters.AddWithValue("@Position", img.Position)
cmd.ExecuteScalar()
End Using
Next
End If
'-- If given article contains articles variations list (artikel_variation_attributes is a list with variations associated with article)
If Not IsNothing(artikel.collection(articleIndex)._artikel_variation_attributes) Then
For Each var In artikel.collection(articleIndex)._artikel_variation_attributes
'--Insert article's images if exists
Using cmd As New SqlCommand("INSERT INTO T_Artikel_T_Variation (FK_Variation_VariationAttribute_ID, FK_Artikel_ID, Position) VALUES (@FK_Variation_VariationAttribute_ID, @FK_Artikel_ID, @Position)", connection)
cmd.CommandType = CommandType.Text
cmd.Connection = connection
cmd.Transaction = transaction
cmd.Parameters.AddWithValue("@FK_Variation_VariationAttribute_ID", New Variation_VariationAttribute(var.FkVariationId, var.FkVariationAttributeId).GetId())
cmd.Parameters.AddWithValue("@FK_Artikel_ID", newArticleRowId)
cmd.Parameters.AddWithValue("@Position", var.Position)
cmd.ExecuteScalar()
End Using
Next
End If
Next
transaction.Commit()
Catch ex As Exception
result = False
'-- Roll the transaction back.
Try
transaction.Rollback()
Catch ex2 As Exception
result = False
End Try
End Try
End Using
Return result
End Function
答案 0 :(得分:0)
我不希望达到你的捕获量。在你的ProcessArticle函数中你有另一个Try .. Catch如果SQL操作中有错误,这就是它将被捕获的地方。然后你返回false!因此调用例程不会捕获错误,因为它已经被优雅地处理。如果要在调用例程中捕获错误,则需要在ProcessArticle函数中从catch中引发错误。
您可以将ProcessArticle中的Try Catch更改为
Catch ex As Exception
result = False
'-- Roll the transaction back.
Try
transaction.Rollback()
Catch ex2 As Exception
Throw New Exception("Rollback failed after first exception.", ex2)
End Try
Throw New Exception("Rollback succeeded after exception.", ex)
End Try
很抱歉,自从我做VB之后已经有一段时间了,但我认为语法是正确的。使用它,调用例程现在应该捕获异常(并且您将有一个内部异常的异常)。您需要检查内部异常以找到原因。