I was working doing a lot of refactoring in a legacy code, and I found this:
Public Function GetDocumentTypes() As DataSet
Dim ds As DataSet = Nothing
Try
ds = SqlHelper.ExecuteDataset(Conn, "USP_DocumentTypes_Get")
Catch ex As Exception
ErrorHandler.Publish(ex, ExceptionDestinationEmail, TSecurity.GetUserID)
Finally
If ds IsNot Nothing Then
ds.Dispose()
End If
End Try
Return ds
End Function
And I started doing a research about how the try catch finally flow works, and I realize that in this case always the ds will be nothing because of the finally statement, then I changed the code to:
Public Function GetDocumentTypes() As DataSet
Dim ds As DataSet = Nothing
Try
ds = SqlHelper.ExecuteDataset(Conn, "USP_DocumentTypes_Get")
Return ds
Catch ex As Exception
ErrorHandler.Publish(ex, ExceptionDestinationEmail, TSecurity.GetUserID)
Finally
If ds IsNot Nothing Then
ds.Dispose()
End If
End Try
End Function
Then started receiving a compiler warning:
At the end I solved the issue writing the code like this:
Public Function GetDocumentTypes() As DataSet
Dim ds As DataSet = Nothing
Try
ds = SqlHelper.ExecuteDataset(Conn, "USP_DocumentTypes_Get")
Return ds
Catch ex As Exception
ErrorHandler.Publish(ex, ExceptionDestinationEmail, TSecurity.GetUserID)
Return ds
Finally
If ds IsNot Nothing Then
ds.Dispose()
End If
End Try
End Function
is that the best way to do it??