3

我在 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

主要问题是代码中到处都可能出现问题,我的意思是不同的程序,不同的形式等。
所以我需要一个通用的解决方案。实际上我什至无法想象一个正确的逻辑来解决这个问题?谁能告诉我一个方向?

4

2 回答 2

2

您可以制作一个需要Action(Of OracleCommand).

在一个块中调用委托Try Catch,如果密码更改,只需再次调用操作以使用正确的密码重新运行代码。

您可以使用 lambda 表达式调用此函数。
请注意,lambda 表达式中的所有内容都可能运行不止一次。

于 2013-02-25T15:25:03.347 回答
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
于 2013-02-25T15:28:41.113 回答