2013-02-25 69 views
3

我在vb.net和Oracle數據庫中有一個項目。當用戶口令在oracle中過期時導致一個oracle的豁免。我處理該異常(打開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 

的主要問題是,這個問題可以出現在代碼無處不在,我的意思是不同的程序,不同的形式等。
所以我需要一個通用的解決方案。其實我甚至無法想象一個正確的邏輯來解決這個問題。任何人都可以告訴我一個方向?

回答

2

您可以製作一個功能,需要Action(Of OracleCommand)

Try Catch塊中調用代理,並且如果密碼更改,只需再次調用該操作以使用正確的密碼重新運行代碼。

你可以用lambda表達式來調用這個函數。
請注意,lambda表達式中的所有內容都可能運行多次。

+0

我不是那麼熟悉的代表和lamdas表達。你可以說得更詳細點嗎? – Nianios 2013-02-25 15:34:04

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 
+0

問題是,您不知道何時何地出現錯誤。 – Nianios 2013-02-25 15:33:14

+1

您可以根據需要添加儘可能多的'try catch語句'。密碼錯誤只會在登錄時發生,而且由於您正在處理特定錯誤,因此您應該知道錯誤發生的位置。 – 2013-02-25 15:39:41