When i use the below function its showing the error message but still it give a error saying that the exception not unhandled. why is it?
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
You exception handler is correctly catching the
SQLException.The problem is in the following line in your handler:
If you want the message box, then just use
If you then want to bubble the exception you should then use the line
Don’t put the
exon the end of the Throw as this creates a new exception (specifically the full stacktrace) instead of rethrowing the old one, thereby masking some details of the actual problem.In addition. If you do choose to rethrow the exception, you then need to catch it again somewhere further up the stack, otherwise you’ll still get the unhandled exception messages.