Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Try Catch inside of functions

Try Catch inside of functions

Scheduled Pinned Locked Moved Visual Basic
data-structuresxmlhelpquestionlounge
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    barney_1972
    wrote on last edited by
    #1

    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

    T S B D 4 Replies Last reply
    0
    • B barney_1972

      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

      T Offline
      T Offline
      Tamimi Code
      wrote on last edited by
      #2

      Dim XmlOutputDocument As New Xml.XmlDocument
      try
      'do your code
      catch ex As Exception
      'error message
      end try
      Return XmlOutputDocument

      When you get mad...THINK twice that the only advice Tamimi - Code

      1 Reply Last reply
      0
      • B barney_1972

        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

        S Offline
        S Offline
        SHatchard
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • B barney_1972

          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

          B Offline
          B Offline
          barney_1972
          wrote on last edited by
          #4

          Many thanks , think i'll try applying the return at the end of the function.

          1 Reply Last reply
          0
          • B barney_1972

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups