J
James Radke
Hello,
I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module (note that this class module represents the business layer of code NOT the gui layer):
Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader
' Declare the SQL data layer class
Dim oSQL As New SqlService(ConnectionString)
' Add the parameters to the command object
oSQL.AddParameter("@Parm1", SqlDbType.SmallInt, 0, Parm1, ParameterDirection.Input)
oSQL.AddParameter("@Parm2", SqlDbType.SmallInt, 0, Parm2, ParameterDirection.Input)
' Run the stored procedure and return a datareader
Return oSQL.RunProcReader("SQLStoredProcedureName")
End Function
which is calling the following library function (data access layer of code) which can/could throw an exception (which I believe already has valid error handling in it):
Public Function RunProcReader(ByVal sProcName As String) As SqlDataReader
' Check to make sure we have all the data necessary for a connection
If Not IsValidConnectionString() Then
Throw New InvalidConnectionException("Invalid SQL connection string: " & ConnectionString)
End If
Dim dr As SqlDataReader '//--- Create a new sqlDataReader
Dim oCmd As SqlCommand = New SqlCommand '//--- Create a new SqlCommand
Dim oCn As SqlConnection = Nothing '//--- Declare the SqlConnection
Dim oSqlParameter As SqlParameter = Nothing '//--- Declare a SqlParameter
Dim oP As Parameter = Nothing '//--- Declare a Parameter
'Get an enumerator for the parameter array list
Dim oEnumerator As IEnumerator = m_oParmList.GetEnumerator()
Try
'Prepare connection to the database
oCn = Connect()
With oCmd
.Connection = oCn
.CommandText = sProcName
.CommandType = CommandType.StoredProcedure
End With
'Add the parameters to the command
AddParameters(oEnumerator, oCmd)
' Open the connection
oCn.Open()
' Execute the datareader, and close the connection
dr = oCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
Throw
Finally
Disconnect(oCn)
End Try
'Get the output parameter values if there were no errors
GetOutputParameterValues(oCmd)
'Return the DataReader
Return dr
End Function
How would we add error handling into the Test function above to be able to gracefully handle the errors encountered in the data access layer (RunProcReader) AND to gracefully send the error back to the gui calling program? Do we just simply enclose everything in a try catch, and then also enclose the call to the Test function (from the GUI layer) within a try catch block so that if an error is encountered we can gracefully handle the presentation?
Anyone care to provide us with ideas as to what you are doing in similar circumstances?
Jim
I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module (note that this class module represents the business layer of code NOT the gui layer):
Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader
' Declare the SQL data layer class
Dim oSQL As New SqlService(ConnectionString)
' Add the parameters to the command object
oSQL.AddParameter("@Parm1", SqlDbType.SmallInt, 0, Parm1, ParameterDirection.Input)
oSQL.AddParameter("@Parm2", SqlDbType.SmallInt, 0, Parm2, ParameterDirection.Input)
' Run the stored procedure and return a datareader
Return oSQL.RunProcReader("SQLStoredProcedureName")
End Function
which is calling the following library function (data access layer of code) which can/could throw an exception (which I believe already has valid error handling in it):
Public Function RunProcReader(ByVal sProcName As String) As SqlDataReader
' Check to make sure we have all the data necessary for a connection
If Not IsValidConnectionString() Then
Throw New InvalidConnectionException("Invalid SQL connection string: " & ConnectionString)
End If
Dim dr As SqlDataReader '//--- Create a new sqlDataReader
Dim oCmd As SqlCommand = New SqlCommand '//--- Create a new SqlCommand
Dim oCn As SqlConnection = Nothing '//--- Declare the SqlConnection
Dim oSqlParameter As SqlParameter = Nothing '//--- Declare a SqlParameter
Dim oP As Parameter = Nothing '//--- Declare a Parameter
'Get an enumerator for the parameter array list
Dim oEnumerator As IEnumerator = m_oParmList.GetEnumerator()
Try
'Prepare connection to the database
oCn = Connect()
With oCmd
.Connection = oCn
.CommandText = sProcName
.CommandType = CommandType.StoredProcedure
End With
'Add the parameters to the command
AddParameters(oEnumerator, oCmd)
' Open the connection
oCn.Open()
' Execute the datareader, and close the connection
dr = oCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
Throw
Finally
Disconnect(oCn)
End Try
'Get the output parameter values if there were no errors
GetOutputParameterValues(oCmd)
'Return the DataReader
Return dr
End Function
How would we add error handling into the Test function above to be able to gracefully handle the errors encountered in the data access layer (RunProcReader) AND to gracefully send the error back to the gui calling program? Do we just simply enclose everything in a try catch, and then also enclose the call to the Test function (from the GUI layer) within a try catch block so that if an error is encountered we can gracefully handle the presentation?
Anyone care to provide us with ideas as to what you are doing in similar circumstances?
Jim