Try Catch inside of functions
-
Some help on good practice would be appreciated. I have the following function and similar functions, but often get a squigly line somewhere in the functions which tells me the function does not return a value on all paths. Public Function ConvertToXmlDocument() As XmlDocument 'Alter the return type to suit - either stream or file or other Dim FileLineString As String Dim LineNumberInteger As Int32 Dim XmlSequenceCount As Int32 'Create xml envelope Dim XmlOutputDocument As New Xml.XmlDocument Try 'Intialise Xml Document If InitialiseXmlDocument(XmlOutputDocument, "Flat file" Then 'Loop array / reading each line of file content For Each FileLineString In Me.FileContentString 'Line count LineNumberInteger += 1 'Retrieve xml node....... 'Unformat retrieved node against file line.... Next Return XmlOutputDocument End If Catch ex As Exception Return Nothing End Try End Function Should i use multiple returns (ie another return statement after the end try) or is their a much better way in general, not just specific to this function? Many Thanks
-
Some help on good practice would be appreciated. I have the following function and similar functions, but often get a squigly line somewhere in the functions which tells me the function does not return a value on all paths. Public Function ConvertToXmlDocument() As XmlDocument 'Alter the return type to suit - either stream or file or other Dim FileLineString As String Dim LineNumberInteger As Int32 Dim XmlSequenceCount As Int32 'Create xml envelope Dim XmlOutputDocument As New Xml.XmlDocument Try 'Intialise Xml Document If InitialiseXmlDocument(XmlOutputDocument, "Flat file" Then 'Loop array / reading each line of file content For Each FileLineString In Me.FileContentString 'Line count LineNumberInteger += 1 'Retrieve xml node....... 'Unformat retrieved node against file line.... Next Return XmlOutputDocument End If Catch ex As Exception Return Nothing End Try End Function Should i use multiple returns (ie another return statement after the end try) or is their a much better way in general, not just specific to this function? Many Thanks
Dim XmlOutputDocument As New Xml.XmlDocument
try
'do your code
catch ex As Exception
'error message
end try
Return XmlOutputDocumentWhen you get mad...THINK twice that the only advice Tamimi - Code
-
Some help on good practice would be appreciated. I have the following function and similar functions, but often get a squigly line somewhere in the functions which tells me the function does not return a value on all paths. Public Function ConvertToXmlDocument() As XmlDocument 'Alter the return type to suit - either stream or file or other Dim FileLineString As String Dim LineNumberInteger As Int32 Dim XmlSequenceCount As Int32 'Create xml envelope Dim XmlOutputDocument As New Xml.XmlDocument Try 'Intialise Xml Document If InitialiseXmlDocument(XmlOutputDocument, "Flat file" Then 'Loop array / reading each line of file content For Each FileLineString In Me.FileContentString 'Line count LineNumberInteger += 1 'Retrieve xml node....... 'Unformat retrieved node against file line.... Next Return XmlOutputDocument End If Catch ex As Exception Return Nothing End Try End Function Should i use multiple returns (ie another return statement after the end try) or is their a much better way in general, not just specific to this function? Many Thanks
The 'function does not return a value on all paths' is happening because you have code like... Try If x=y Then ... Return x End If Catch Return Nothing End Try But if you note if the 'If' clause is not met then nothing will be returned, so you should implement something similar to Try If x=y Then ... Return x Else Return y End If Catch Return Nothing End Try
-
Some help on good practice would be appreciated. I have the following function and similar functions, but often get a squigly line somewhere in the functions which tells me the function does not return a value on all paths. Public Function ConvertToXmlDocument() As XmlDocument 'Alter the return type to suit - either stream or file or other Dim FileLineString As String Dim LineNumberInteger As Int32 Dim XmlSequenceCount As Int32 'Create xml envelope Dim XmlOutputDocument As New Xml.XmlDocument Try 'Intialise Xml Document If InitialiseXmlDocument(XmlOutputDocument, "Flat file" Then 'Loop array / reading each line of file content For Each FileLineString In Me.FileContentString 'Line count LineNumberInteger += 1 'Retrieve xml node....... 'Unformat retrieved node against file line.... Next Return XmlOutputDocument End If Catch ex As Exception Return Nothing End Try End Function Should i use multiple returns (ie another return statement after the end try) or is their a much better way in general, not just specific to this function? Many Thanks
Many thanks , think i'll try applying the return at the end of the function.
-
Some help on good practice would be appreciated. I have the following function and similar functions, but often get a squigly line somewhere in the functions which tells me the function does not return a value on all paths. Public Function ConvertToXmlDocument() As XmlDocument 'Alter the return type to suit - either stream or file or other Dim FileLineString As String Dim LineNumberInteger As Int32 Dim XmlSequenceCount As Int32 'Create xml envelope Dim XmlOutputDocument As New Xml.XmlDocument Try 'Intialise Xml Document If InitialiseXmlDocument(XmlOutputDocument, "Flat file" Then 'Loop array / reading each line of file content For Each FileLineString In Me.FileContentString 'Line count LineNumberInteger += 1 'Retrieve xml node....... 'Unformat retrieved node against file line.... Next Return XmlOutputDocument End If Catch ex As Exception Return Nothing End Try End Function Should i use multiple returns (ie another return statement after the end try) or is their a much better way in general, not just specific to this function? Many Thanks
I believe in the concept that a function should have but a single Return statement. Create a variable in the beginning of the function and set it's default return value. The rest of the function code should work on validating input parameters and changing that default value or throwing an exception if something goes wrong. By the time the execution gets to the bottom of the function, the return statement has the value that needs to go back to the caller.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007