我有以下Try语句,但是当写入DB的值太大时,所有内容都没有运行。 我应该使用Exit Try而不是Exit Sub吗?
Try
Dim conn As OracleConnection
Dim cmdProduction As New OracleCommand
conn = GetConnect()
conn.Open()
cmdProduction.Connection = conn
cmdProduction.CommandText = "INSERT INTO PRODUCTION (DateTime, Plant, Runhours, Tons, Shift, Sft, Plant_Ord) Values ('" & DateTime & "','FM1-Selox','" & (seloxRuntime / 60) & "','" & seloxTons & "','" & theShift & "','" & sft & "','51')"
cmdProduction.ExecuteNonQuery()
Catch ex As Exception
WriteToFile("Production - Production : Database Error : " & ex.Message)
conn.Close()
Exit Sub
Finally
conn.Close()
End Try
答案 0 :(得分:4)
您也不需要,从conn.Close
删除Exit Sub
和Catch
,您就可以了。 conn.Close
将从Finally
执行。
Try
'
'
'
Catch ex As Exception
WriteToFile("Production - Production : Database Error : " & ex.Message)
Finally
conn.Close()
End Try
当然,如果您的示例代码不完整,实际上看起来像这样:
Finally
conn.Close()
End Try
' More code
然后使用Exit Sub
,或将剩余的代码移到Try
。
Try
'
'
'
' More code
Catch ex As Exception
WriteToFile("Production - Production : Database Error : " & ex.Message)
Finally
conn.Close()
End Try