2012-01-15 44 views
0

当我使用下面的函数显示错误消息,但它仍然给出一个错误,说明异常不处理。为什么?在执行存储过程中的异常处理

Public Function DepartmentDelete(ByVal DepartmentID As Integer) As DataTable 

    Try 
     Using con As New SqlConnection(CMClass.GetConnectionString()) 
      Dim ds As DataTable = New DataTable 
      con.Open() 
      Dim command As SqlCommand = New SqlCommand("Department_Delete", con) 
      command.Parameters.AddWithValue("@DepartmentID", DepartmentID) 
      command.CommandType = CommandType.StoredProcedure 
      Dim adapter As SqlDataAdapter = New SqlDataAdapter(command) 
      Dim table As DataTable = New DataTable 
      adapter.Fill(ds) 
      Return ds 
      con.Close() 
     End Using 
    Catch ex As SqlException 
       Throw New Exception(MsgBox(ex.Message)) 
     End 
    End Try 

End Function 
+0

你可以请你发布你的例外吗? – 2012-01-15 11:55:47

回答

1

您异常处理程序正确捕获SQLException

的问题是在处理函数中的以下行:

Throw New Exception(MsgBox(ex.Message)) 

如果你想在消息框,然后只使用

MsgBox(ex.Message) 

然后,如果您想泡例外,你应该再使用该行

Throw 

不要把ex放在抛出a的末尾这会创建一个新的异常(特别是完整的堆栈跟踪)而不是重新抛出旧的异常,从而掩盖了实际问题的一些细节。

另外。如果你选择重新抛出异常,那么你需要在堆栈的更上方再捕捉它,否则你仍然会得到未处理的异常消息。

相关问题