我在vb.net和Oracle数据库中有一个项目。当用户密码在oracle中过期时导致oracle exeption。我处理该异常(打开一个ChangePassword表单并更改密码)但是当我发现错误时,我正在丢失我准备执行的程序:
例如:
Try
conn = New OracleConnection
conn.ConnectionString = gApp.ConnectString
conn.Open()
'Let's say that error appears here, I want to retun here after password has been changed
cmd.Connection = conn
cmd.CommandText = "Delete_Transaction"
cmd.CommandType = CommandType.StoredProcedure
OracleCommandBuilder.DeriveParameters(cmd)
cmd.Parameters("in_transaction_id").Value = TransactionId
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As OracleException
'PseudoCode: if error is PasswordExpired->open the ChengePassForm...blah,blah
'is handled by a Error class that i have
Finally
If Not conn Is Nothing Then
conn.Dispose()
End If
End Try
主要问题是问题可能出现在代码的各处,我指的是不同的程序,不同的形式等 所以我需要通用的解决方案。实际上我甚至无法想象一个正确的逻辑来接近这个?有谁能告诉我一个方向?
答案 0 :(得分:2)
您可以创建一个Action(Of OracleCommand)
。
在Try Catch
块中调用委托,如果密码更改,只需再次调用该操作以使用正确的密码重新运行代码。
您可以使用lambda表达式调用此函数 请注意lambda表达式中的所有内容可能会多次运行。
答案 1 :(得分:1)
使用嵌套的try catch语句:
Try
Try
conn = New OracleConnection
conn.ConnectionString = gApp.ConnectString
conn.Open()
'Let's say that error appears here, I want to retun here after password has been changed
Catch ex1 As OracleException
'PseudoCode: if error is PasswordExpired->open the ChengePassForm...blah,blah
'is handled by a Error class that i have
Catch ex2 As Exception
' throw ex2 to be handled by the parent try catch
End Try
cmd.Connection = conn
cmd.CommandText = "Delete_Transaction"
cmd.CommandType = CommandType.StoredProcedure
OracleCommandBuilder.DeriveParameters(cmd)
cmd.Parameters("in_transaction_id").Value = TransactionId
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
'PseudoCode: if error is PasswordExpired->open the ChengePassForm...blah,blah
'is handled by a Error class that i have
Finally
If Not conn Is Nothing Then
conn.Dispose()
End If
End Try