Try Catch question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Woody Splawn

    Try Catch question

    I have a try catch statement in a fucntion that is supposed to return a true
    or a false

    My code looks like this:

    Try
    mySqlConnection .Open()
    Dim Da1 As New SqlDataAdapter( "Select JnlType, Description from
    JnlType", mySqlConnection )
    Dim Ds As New DataSet("X")
    Da1.Fill(Ds)
    Catch
    MsgBox("There was a problem filling the Dataset for Lookup table
    for JnlType")
    Return False
    Finally
    If mySqlConnection .State = ConnectionState .Open Then
    mySqlConnection .Close()
    End If
    End Try

    My question is this: Is the Try Catch code smart enough to run the Finally
    statment after the MsgBox and then return false or do I need to write the
    code some other way. This is what I want it to do but I am not sure I have
    it written correctly.



  • William Ryan  eMVP

    #2
    Re: Try Catch question

    Finally will always be executed and yes, it will still return false the way
    you wrote it.
    "Woody Splawn" <woody@splawns. com> wrote in message
    news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I have a try catch statement in a fucntion that is supposed to return a[/color]
    true[color=blue]
    > or a false
    >
    > My code looks like this:
    >
    > Try
    > mySqlConnection .Open()
    > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
    from[color=blue]
    > JnlType", mySqlConnection )
    > Dim Ds As New DataSet("X")
    > Da1.Fill(Ds)
    > Catch
    > MsgBox("There was a problem filling the Dataset for Lookup[/color]
    table[color=blue]
    > for JnlType")
    > Return False
    > Finally
    > If mySqlConnection .State = ConnectionState .Open Then
    > mySqlConnection .Close()
    > End If
    > End Try
    >
    > My question is this: Is the Try Catch code smart enough to run the[/color]
    Finally[color=blue]
    > statment after the MsgBox and then return false or do I need to write the
    > code some other way. This is what I want it to do but I am not sure I[/color]
    have[color=blue]
    > it written correctly.
    >
    >
    >[/color]


    Comment

    • Tom Leylan

      #3
      Re: Try Catch question

      William... I believe we need to clarify something. It isn't going to
      return FALSE because of the way he wrote it. It is going to return FALSE
      because it ends up FALSE when no return value has been set.


      Woody... I don't think you want it to just "accidental ly" work. Test it
      yourself but (as part of the test) have a function return an integer so you
      can get more than 2 return values.

      Unless I'm greatly mistaken it works as follows:

      You want two return statements in your code: Return True in the Try block
      and Return False after the End Try. With no return in Catch or Finally.

      If there is no error it will return the value in the Try block (TRUE) if
      there is an error the Catch block executes, the the Finally and you will
      return the value of the Return statement (FALSE).

      Interestingly even if you place a return statement in the Catch block it
      will not be returned. Either the Try return works or the final return does.

      Test it out...
      Tom



      "William Ryan eMVP" <bill@NoSp4m.de vbuzz.com> wrote in message
      news:uFrMFxr6DH A.2952@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Finally will always be executed and yes, it will still return false the[/color]
      way[color=blue]
      > you wrote it.
      > "Woody Splawn" <woody@splawns. com> wrote in message
      > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...[color=green]
      > > I have a try catch statement in a fucntion that is supposed to return a[/color]
      > true[color=green]
      > > or a false
      > >
      > > My code looks like this:
      > >
      > > Try
      > > mySqlConnection .Open()
      > > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
      > from[color=green]
      > > JnlType", mySqlConnection )
      > > Dim Ds As New DataSet("X")
      > > Da1.Fill(Ds)
      > > Catch
      > > MsgBox("There was a problem filling the Dataset for Lookup[/color]
      > table[color=green]
      > > for JnlType")
      > > Return False
      > > Finally
      > > If mySqlConnection .State = ConnectionState .Open Then
      > > mySqlConnection .Close()
      > > End If
      > > End Try
      > >
      > > My question is this: Is the Try Catch code smart enough to run the[/color]
      > Finally[color=green]
      > > statment after the MsgBox and then return false or do I need to write[/color][/color]
      the[color=blue][color=green]
      > > code some other way. This is what I want it to do but I am not sure I[/color]
      > have[color=green]
      > > it written correctly.
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • William Ryan  eMVP

        #4
        Re: Try Catch question

        Tom:

        Good call, and yes you're absolutely correct. I realize my answer may have
        been misleading -- I was answering it in the context of if an exception was
        caused b/c it seemed he was asking about the behavior of Finally. However,
        if you don't return a value in this instance, it will in fact return false.
        That's more a function of how VB.NET can allow you to not explicitly return
        a value in a function but you have a good point nonetheless.

        I'll be more clear next time.

        bill
        "Tom Leylan" <gee@iamtiredof spam.com> wrote in message
        news:eOf9qQs6DH A.1936@TK2MSFTN GP12.phx.gbl...[color=blue]
        > William... I believe we need to clarify something. It isn't going to
        > return FALSE because of the way he wrote it. It is going to return FALSE
        > because it ends up FALSE when no return value has been set.
        >
        >
        > Woody... I don't think you want it to just "accidental ly" work. Test it
        > yourself but (as part of the test) have a function return an integer so[/color]
        you[color=blue]
        > can get more than 2 return values.
        >
        > Unless I'm greatly mistaken it works as follows:
        >
        > You want two return statements in your code: Return True in the Try block
        > and Return False after the End Try. With no return in Catch or Finally.
        >
        > If there is no error it will return the value in the Try block (TRUE) if
        > there is an error the Catch block executes, the the Finally and you will
        > return the value of the Return statement (FALSE).
        >
        > Interestingly even if you place a return statement in the Catch block it
        > will not be returned. Either the Try return works or the final return[/color]
        does.[color=blue]
        >
        > Test it out...
        > Tom
        >
        >
        >
        > "William Ryan eMVP" <bill@NoSp4m.de vbuzz.com> wrote in message
        > news:uFrMFxr6DH A.2952@TK2MSFTN GP09.phx.gbl...[color=green]
        > > Finally will always be executed and yes, it will still return false the[/color]
        > way[color=green]
        > > you wrote it.
        > > "Woody Splawn" <woody@splawns. com> wrote in message
        > > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...[color=darkred]
        > > > I have a try catch statement in a fucntion that is supposed to return[/color][/color][/color]
        a[color=blue][color=green]
        > > true[color=darkred]
        > > > or a false
        > > >
        > > > My code looks like this:
        > > >
        > > > Try
        > > > mySqlConnection .Open()
        > > > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
        > > from[color=darkred]
        > > > JnlType", mySqlConnection )
        > > > Dim Ds As New DataSet("X")
        > > > Da1.Fill(Ds)
        > > > Catch
        > > > MsgBox("There was a problem filling the Dataset for Lookup[/color]
        > > table[color=darkred]
        > > > for JnlType")
        > > > Return False
        > > > Finally
        > > > If mySqlConnection .State = ConnectionState .Open Then
        > > > mySqlConnection .Close()
        > > > End If
        > > > End Try
        > > >
        > > > My question is this: Is the Try Catch code smart enough to run the[/color]
        > > Finally[color=darkred]
        > > > statment after the MsgBox and then return false or do I need to write[/color][/color]
        > the[color=green][color=darkred]
        > > > code some other way. This is what I want it to do but I am not sure I[/color]
        > > have[color=darkred]
        > > > it written correctly.
        > > >
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Brian

          #5
          Re: Try Catch question

          Woody, you wrote it correctly. It will return False because all the code in
          your catch block will run. It doesn't matter if you have a Finally or not. I
          wouldn't change a thing. It works perfectly.


          "Woody Splawn" <woody@splawns. com> wrote in message
          news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...[color=blue]
          > I have a try catch statement in a fucntion that is supposed to return a[/color]
          true[color=blue]
          > or a false
          >
          > My code looks like this:
          >
          > Try
          > mySqlConnection .Open()
          > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
          from[color=blue]
          > JnlType", mySqlConnection )
          > Dim Ds As New DataSet("X")
          > Da1.Fill(Ds)
          > Catch
          > MsgBox("There was a problem filling the Dataset for Lookup[/color]
          table[color=blue]
          > for JnlType")
          > Return False
          > Finally
          > If mySqlConnection .State = ConnectionState .Open Then
          > mySqlConnection .Close()
          > End If
          > End Try
          >
          > My question is this: Is the Try Catch code smart enough to run the[/color]
          Finally[color=blue]
          > statment after the MsgBox and then return false or do I need to write the
          > code some other way. This is what I want it to do but I am not sure I[/color]
          have[color=blue]
          > it written correctly.
          >
          >
          >[/color]


          Comment

          • Brian

            #6
            Re: Try Catch question

            Oops, I would change one thing: Put a Return True after Da1.Fill(Ds). Then
            it should work correctly.

            Try
            mySqlConnection .Open()
            Dim Da1 As New SqlDataAdapter( "Select JnlType, Description from
            JnlType", mySqlConnection )
            Dim Ds As New DataSet("X")
            Da1.Fill(Ds)
            Return True <-------------------ADD THIS
            Catch
            MsgBox("There was a problem filling the Dataset for Lookup table
            for JnlType")
            Return False
            Finally
            If mySqlConnection .State = ConnectionState .Open Then
            mySqlConnection .Close()
            End If
            End Try


            "Brian" <nospam@prairie .lakes.com> wrote in message
            news:1020n4u6oc 3sf8c@corp.supe rnews.com...[color=blue]
            > Woody, you wrote it correctly. It will return False because all the code[/color]
            in[color=blue]
            > your catch block will run. It doesn't matter if you have a Finally or not.[/color]
            I[color=blue]
            > wouldn't change a thing. It works perfectly.
            >
            >
            > "Woody Splawn" <woody@splawns. com> wrote in message
            > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...[color=green]
            > > I have a try catch statement in a fucntion that is supposed to return a[/color]
            > true[color=green]
            > > or a false
            > >
            > > My code looks like this:
            > >
            > > Try
            > > mySqlConnection .Open()
            > > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
            > from[color=green]
            > > JnlType", mySqlConnection )
            > > Dim Ds As New DataSet("X")
            > > Da1.Fill(Ds)
            > > Catch
            > > MsgBox("There was a problem filling the Dataset for Lookup[/color]
            > table[color=green]
            > > for JnlType")
            > > Return False
            > > Finally
            > > If mySqlConnection .State = ConnectionState .Open Then
            > > mySqlConnection .Close()
            > > End If
            > > End Try
            > >
            > > My question is this: Is the Try Catch code smart enough to run the[/color]
            > Finally[color=green]
            > > statment after the MsgBox and then return false or do I need to write[/color][/color]
            the[color=blue][color=green]
            > > code some other way. This is what I want it to do but I am not sure I[/color]
            > have[color=green]
            > > it written correctly.
            > >
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Tom Leylan

              #7
              Re: Try Catch question

              Test it Brian... your I believe you will find that your Return False doesn't
              execute.


              "Brian" <nospam@prairie .lakes.com> wrote in message
              news:1020njo2tq 0v786@corp.supe rnews.com...[color=blue]
              > Oops, I would change one thing: Put a Return True after Da1.Fill(Ds). Then
              > it should work correctly.
              >
              > Try
              > mySqlConnection .Open()
              > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
              from[color=blue]
              > JnlType", mySqlConnection )
              > Dim Ds As New DataSet("X")
              > Da1.Fill(Ds)
              > Return True <-------------------ADD THIS
              > Catch
              > MsgBox("There was a problem filling the Dataset for Lookup table
              > for JnlType")
              > Return False
              > Finally
              > If mySqlConnection .State = ConnectionState .Open Then
              > mySqlConnection .Close()
              > End If
              > End Try
              >
              >
              > "Brian" <nospam@prairie .lakes.com> wrote in message
              > news:1020n4u6oc 3sf8c@corp.supe rnews.com...[color=green]
              > > Woody, you wrote it correctly. It will return False because all the code[/color]
              > in[color=green]
              > > your catch block will run. It doesn't matter if you have a Finally or[/color][/color]
              not.[color=blue]
              > I[color=green]
              > > wouldn't change a thing. It works perfectly.
              > >
              > >
              > > "Woody Splawn" <woody@splawns. com> wrote in message
              > > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...[color=darkred]
              > > > I have a try catch statement in a fucntion that is supposed to return[/color][/color][/color]
              a[color=blue][color=green]
              > > true[color=darkred]
              > > > or a false
              > > >
              > > > My code looks like this:
              > > >
              > > > Try
              > > > mySqlConnection .Open()
              > > > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
              > > from[color=darkred]
              > > > JnlType", mySqlConnection )
              > > > Dim Ds As New DataSet("X")
              > > > Da1.Fill(Ds)
              > > > Catch
              > > > MsgBox("There was a problem filling the Dataset for Lookup[/color]
              > > table[color=darkred]
              > > > for JnlType")
              > > > Return False
              > > > Finally
              > > > If mySqlConnection .State = ConnectionState .Open Then
              > > > mySqlConnection .Close()
              > > > End If
              > > > End Try
              > > >
              > > > My question is this: Is the Try Catch code smart enough to run the[/color]
              > > Finally[color=darkred]
              > > > statment after the MsgBox and then return false or do I need to write[/color][/color]
              > the[color=green][color=darkred]
              > > > code some other way. This is what I want it to do but I am not sure I[/color]
              > > have[color=darkred]
              > > > it written correctly.
              > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Brian

                #8
                Re: Try Catch question

                I did. It worked perfectly. Now it's your turn to test it. Use the debugger
                to step thru the code.

                Private Function Test() As Boolean
                Dim g As Integer = 5
                Try
                g \= 0 '<----------Force an exception , will return false.
                Then change it to g \= 1 will return True
                Return True
                Catch ex As Exception
                MessageBox.Show ("Caught")
                Return False
                Finally
                MessageBox.Show ("Finally")
                End Try

                End Function



                "Tom Leylan" <gee@iamtiredof spam.com> wrote in message
                news:OG%23lVvs6 DHA.488@TK2MSFT NGP12.phx.gbl.. .[color=blue]
                > Test it Brian... your I believe you will find that your Return False[/color]
                doesn't[color=blue]
                > execute.
                >
                >
                > "Brian" <nospam@prairie .lakes.com> wrote in message
                > news:1020njo2tq 0v786@corp.supe rnews.com...[color=green]
                > > Oops, I would change one thing: Put a Return True after Da1.Fill(Ds).[/color][/color]
                Then[color=blue][color=green]
                > > it should work correctly.
                > >
                > > Try
                > > mySqlConnection .Open()
                > > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
                > from[color=green]
                > > JnlType", mySqlConnection )
                > > Dim Ds As New DataSet("X")
                > > Da1.Fill(Ds)
                > > Return True <-------------------ADD THIS
                > > Catch
                > > MsgBox("There was a problem filling the Dataset for Lookup[/color][/color]
                table[color=blue][color=green]
                > > for JnlType")
                > > Return False
                > > Finally
                > > If mySqlConnection .State = ConnectionState .Open Then
                > > mySqlConnection .Close()
                > > End If
                > > End Try
                > >
                > >
                > > "Brian" <nospam@prairie .lakes.com> wrote in message
                > > news:1020n4u6oc 3sf8c@corp.supe rnews.com...[color=darkred]
                > > > Woody, you wrote it correctly. It will return False because all the[/color][/color][/color]
                code[color=blue][color=green]
                > > in[color=darkred]
                > > > your catch block will run. It doesn't matter if you have a Finally or[/color][/color]
                > not.[color=green]
                > > I[color=darkred]
                > > > wouldn't change a thing. It works perfectly.
                > > >
                > > >
                > > > "Woody Splawn" <woody@splawns. com> wrote in message
                > > > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...
                > > > > I have a try catch statement in a fucntion that is supposed to[/color][/color][/color]
                return[color=blue]
                > a[color=green][color=darkred]
                > > > true
                > > > > or a false
                > > > >
                > > > > My code looks like this:
                > > > >
                > > > > Try
                > > > > mySqlConnection .Open()
                > > > > Dim Da1 As New SqlDataAdapter( "Select JnlType,[/color][/color][/color]
                Description[color=blue][color=green][color=darkred]
                > > > from
                > > > > JnlType", mySqlConnection )
                > > > > Dim Ds As New DataSet("X")
                > > > > Da1.Fill(Ds)
                > > > > Catch
                > > > > MsgBox("There was a problem filling the Dataset for[/color][/color][/color]
                Lookup[color=blue][color=green][color=darkred]
                > > > table
                > > > > for JnlType")
                > > > > Return False
                > > > > Finally
                > > > > If mySqlConnection .State = ConnectionState .Open Then
                > > > > mySqlConnection .Close()
                > > > > End If
                > > > > End Try
                > > > >
                > > > > My question is this: Is the Try Catch code smart enough to run the
                > > > Finally
                > > > > statment after the MsgBox and then return false or do I need to[/color][/color][/color]
                write[color=blue][color=green]
                > > the[color=darkred]
                > > > > code some other way. This is what I want it to do but I am not sure[/color][/color][/color]
                I[color=blue][color=green][color=darkred]
                > > > have
                > > > > it written correctly.
                > > > >
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Tom Leylan

                  #9
                  Re: Try Catch question

                  Seriously I wouldn't have mentioned you trying it if I hadn't :-) I didn't
                  step through it with the debugger but I did set return values. This time
                  however it is returning the return value in the catch block... I mentioned
                  in another reply that it didn't matter. Can't imagine what I did
                  differently.

                  Let me post my code and Woody can decide what he prefers. While it works as
                  you posted it something seems odd about "return" which is supposed to exit
                  immediately suddenly not exiting. Clearly the error handler has deferred it
                  but perhaps that is why Woody was uncertain. What I've done is simply use
                  the Function name which as we know will return the value and does not cause
                  an immediate exit (in the normal case) so it behaves identically whether or
                  not an error has occurred.

                  You are correct however that it works fine the way you posted it. I don't
                  know what I did in my first test...
                  Tom


                  Public Function ErrorSub() As Boolean

                  Try
                  Throw New System.Exceptio n("error")
                  ErrorSub = True

                  Catch ex As Exception
                  MsgBox("catch", MsgBoxStyle.OKO nly, "error")
                  ErrorSub = False

                  Finally
                  MsgBox("finally ", MsgBoxStyle.OKO nly, "error")

                  End Try

                  End Function



                  "Brian" <nospam@prairie .lakes.com> wrote in message
                  news:1020prqf2t 3cidd@corp.supe rnews.com...[color=blue]
                  > I did. It worked perfectly. Now it's your turn to test it. Use the[/color]
                  debugger[color=blue]
                  > to step thru the code.
                  >
                  > Private Function Test() As Boolean
                  > Dim g As Integer = 5
                  > Try
                  > g \= 0 '<----------Force an exception , will return false.
                  > Then change it to g \= 1 will return True
                  > Return True
                  > Catch ex As Exception
                  > MessageBox.Show ("Caught")
                  > Return False
                  > Finally
                  > MessageBox.Show ("Finally")
                  > End Try
                  >
                  > End Function
                  >
                  >
                  >
                  > "Tom Leylan" <gee@iamtiredof spam.com> wrote in message
                  > news:OG%23lVvs6 DHA.488@TK2MSFT NGP12.phx.gbl.. .[color=green]
                  > > Test it Brian... your I believe you will find that your Return False[/color]
                  > doesn't[color=green]
                  > > execute.
                  > >
                  > >
                  > > "Brian" <nospam@prairie .lakes.com> wrote in message
                  > > news:1020njo2tq 0v786@corp.supe rnews.com...[color=darkred]
                  > > > Oops, I would change one thing: Put a Return True after Da1.Fill(Ds).[/color][/color]
                  > Then[color=green][color=darkred]
                  > > > it should work correctly.
                  > > >
                  > > > Try
                  > > > mySqlConnection .Open()
                  > > > Dim Da1 As New SqlDataAdapter( "Select JnlType, Description[/color]
                  > > from[color=darkred]
                  > > > JnlType", mySqlConnection )
                  > > > Dim Ds As New DataSet("X")
                  > > > Da1.Fill(Ds)
                  > > > Return True <-------------------ADD THIS
                  > > > Catch
                  > > > MsgBox("There was a problem filling the Dataset for Lookup[/color][/color]
                  > table[color=green][color=darkred]
                  > > > for JnlType")
                  > > > Return False
                  > > > Finally
                  > > > If mySqlConnection .State = ConnectionState .Open Then
                  > > > mySqlConnection .Close()
                  > > > End If
                  > > > End Try
                  > > >
                  > > >
                  > > > "Brian" <nospam@prairie .lakes.com> wrote in message
                  > > > news:1020n4u6oc 3sf8c@corp.supe rnews.com...
                  > > > > Woody, you wrote it correctly. It will return False because all the[/color][/color]
                  > code[color=green][color=darkred]
                  > > > in
                  > > > > your catch block will run. It doesn't matter if you have a Finally[/color][/color][/color]
                  or[color=blue][color=green]
                  > > not.[color=darkred]
                  > > > I
                  > > > > wouldn't change a thing. It works perfectly.
                  > > > >
                  > > > >
                  > > > > "Woody Splawn" <woody@splawns. com> wrote in message
                  > > > > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...
                  > > > > > I have a try catch statement in a fucntion that is supposed to[/color][/color]
                  > return[color=green]
                  > > a[color=darkred]
                  > > > > true
                  > > > > > or a false
                  > > > > >
                  > > > > > My code looks like this:
                  > > > > >
                  > > > > > Try
                  > > > > > mySqlConnection .Open()
                  > > > > > Dim Da1 As New SqlDataAdapter( "Select JnlType,[/color][/color]
                  > Description[color=green][color=darkred]
                  > > > > from
                  > > > > > JnlType", mySqlConnection )
                  > > > > > Dim Ds As New DataSet("X")
                  > > > > > Da1.Fill(Ds)
                  > > > > > Catch
                  > > > > > MsgBox("There was a problem filling the Dataset for[/color][/color]
                  > Lookup[color=green][color=darkred]
                  > > > > table
                  > > > > > for JnlType")
                  > > > > > Return False
                  > > > > > Finally
                  > > > > > If mySqlConnection .State = ConnectionState .Open Then
                  > > > > > mySqlConnection .Close()
                  > > > > > End If
                  > > > > > End Try
                  > > > > >
                  > > > > > My question is this: Is the Try Catch code smart enough to run[/color][/color][/color]
                  the[color=blue][color=green][color=darkred]
                  > > > > Finally
                  > > > > > statment after the MsgBox and then return false or do I need to[/color][/color]
                  > write[color=green][color=darkred]
                  > > > the
                  > > > > > code some other way. This is what I want it to do but I am not[/color][/color][/color]
                  sure[color=blue]
                  > I[color=green][color=darkred]
                  > > > > have
                  > > > > > it written correctly.
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > >
                  > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Tom Leylan

                    #10
                    Re: Try Catch question

                    Turns out it wasn't such a good call. I swear I tested the code but
                    "something" mixed me up.

                    The Return statement is deferred until after the finally statement
                    executes... the line is executed but it doesn't actually exit at that point.
                    So anyway, it would work but I posted slightly different syntax (using the
                    function name) which makes it clearer (to me) that it is only assigning the
                    value and can't possibly return at that point.

                    Tom


                    "William Ryan eMVP" <bill@NoSp4m.de vbuzz.com> wrote in message
                    news:O3tDues6DH A.1816@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Tom:
                    >
                    > Good call, and yes you're absolutely correct. I realize my answer may[/color]
                    have[color=blue]
                    > been misleading -- I was answering it in the context of if an exception[/color]
                    was[color=blue]
                    > caused b/c it seemed he was asking about the behavior of Finally.[/color]
                    However,[color=blue]
                    > if you don't return a value in this instance, it will in fact return[/color]
                    false.[color=blue]
                    > That's more a function of how VB.NET can allow you to not explicitly[/color]
                    return[color=blue]
                    > a value in a function but you have a good point nonetheless.
                    >
                    > I'll be more clear next time.
                    >
                    > bill
                    > "Tom Leylan" <gee@iamtiredof spam.com> wrote in message
                    > news:eOf9qQs6DH A.1936@TK2MSFTN GP12.phx.gbl...[color=green]
                    > > William... I believe we need to clarify something. It isn't going to
                    > > return FALSE because of the way he wrote it. It is going to return[/color][/color]
                    FALSE[color=blue][color=green]
                    > > because it ends up FALSE when no return value has been set.
                    > >
                    > >
                    > > Woody... I don't think you want it to just "accidental ly" work. Test it
                    > > yourself but (as part of the test) have a function return an integer so[/color]
                    > you[color=green]
                    > > can get more than 2 return values.
                    > >
                    > > Unless I'm greatly mistaken it works as follows:
                    > >
                    > > You want two return statements in your code: Return True in the Try[/color][/color]
                    block[color=blue][color=green]
                    > > and Return False after the End Try. With no return in Catch or Finally.
                    > >
                    > > If there is no error it will return the value in the Try block (TRUE) if
                    > > there is an error the Catch block executes, the the Finally and you will
                    > > return the value of the Return statement (FALSE).
                    > >
                    > > Interestingly even if you place a return statement in the Catch block it
                    > > will not be returned. Either the Try return works or the final return[/color]
                    > does.[color=green]
                    > >
                    > > Test it out...
                    > > Tom
                    > >
                    > >
                    > >
                    > > "William Ryan eMVP" <bill@NoSp4m.de vbuzz.com> wrote in message
                    > > news:uFrMFxr6DH A.2952@TK2MSFTN GP09.phx.gbl...[color=darkred]
                    > > > Finally will always be executed and yes, it will still return false[/color][/color][/color]
                    the[color=blue][color=green]
                    > > way[color=darkred]
                    > > > you wrote it.
                    > > > "Woody Splawn" <woody@splawns. com> wrote in message
                    > > > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...
                    > > > > I have a try catch statement in a fucntion that is supposed to[/color][/color][/color]
                    return[color=blue]
                    > a[color=green][color=darkred]
                    > > > true
                    > > > > or a false
                    > > > >
                    > > > > My code looks like this:
                    > > > >
                    > > > > Try
                    > > > > mySqlConnection .Open()
                    > > > > Dim Da1 As New SqlDataAdapter( "Select JnlType,[/color][/color][/color]
                    Description[color=blue][color=green][color=darkred]
                    > > > from
                    > > > > JnlType", mySqlConnection )
                    > > > > Dim Ds As New DataSet("X")
                    > > > > Da1.Fill(Ds)
                    > > > > Catch
                    > > > > MsgBox("There was a problem filling the Dataset for[/color][/color][/color]
                    Lookup[color=blue][color=green][color=darkred]
                    > > > table
                    > > > > for JnlType")
                    > > > > Return False
                    > > > > Finally
                    > > > > If mySqlConnection .State = ConnectionState .Open Then
                    > > > > mySqlConnection .Close()
                    > > > > End If
                    > > > > End Try
                    > > > >
                    > > > > My question is this: Is the Try Catch code smart enough to run the
                    > > > Finally
                    > > > > statment after the MsgBox and then return false or do I need to[/color][/color][/color]
                    write[color=blue][color=green]
                    > > the[color=darkred]
                    > > > > code some other way. This is what I want it to do but I am not sure[/color][/color][/color]
                    I[color=blue][color=green][color=darkred]
                    > > > have
                    > > > > it written correctly.
                    > > > >
                    > > > >
                    > > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • William Ryan  eMVP

                      #11
                      Re: Try Catch question

                      Tom:

                      Your point is still correct, namely that if you don't specify a return type
                      in your function, it's coming back null. If you look at the original
                      question and context, it will return false if an exception is thrown.
                      VB.NET is 'smart' enough to fire the messagebox, return false and execute
                      the code in the finally statement.

                      I guess another point worth bringing up is that Finally blocks aren't well
                      suited to returning values in many cases if you plan on having a specific
                      return value if an exception(s) is/are raised. Finally clauses lend
                      themselves quite well to cleanup code, streams, connections and the like,
                      but return values aren't really well suited to it. Every MCAD test guide
                      I've seen raises this issue ;-).


                      "Tom Leylan" <gee@iamtiredof spam.com> wrote in message
                      news:%23l%237tq t6DHA.2952@TK2M SFTNGP09.phx.gb l...[color=blue]
                      > Turns out it wasn't such a good call. I swear I tested the code but
                      > "something" mixed me up.
                      >
                      > The Return statement is deferred until after the finally statement
                      > executes... the line is executed but it doesn't actually exit at that[/color]
                      point.[color=blue]
                      > So anyway, it would work but I posted slightly different syntax (using the
                      > function name) which makes it clearer (to me) that it is only assigning[/color]
                      the[color=blue]
                      > value and can't possibly return at that point.
                      >
                      > Tom
                      >
                      >
                      > "William Ryan eMVP" <bill@NoSp4m.de vbuzz.com> wrote in message
                      > news:O3tDues6DH A.1816@TK2MSFTN GP12.phx.gbl...[color=green]
                      > > Tom:
                      > >
                      > > Good call, and yes you're absolutely correct. I realize my answer may[/color]
                      > have[color=green]
                      > > been misleading -- I was answering it in the context of if an exception[/color]
                      > was[color=green]
                      > > caused b/c it seemed he was asking about the behavior of Finally.[/color]
                      > However,[color=green]
                      > > if you don't return a value in this instance, it will in fact return[/color]
                      > false.[color=green]
                      > > That's more a function of how VB.NET can allow you to not explicitly[/color]
                      > return[color=green]
                      > > a value in a function but you have a good point nonetheless.
                      > >
                      > > I'll be more clear next time.
                      > >
                      > > bill
                      > > "Tom Leylan" <gee@iamtiredof spam.com> wrote in message
                      > > news:eOf9qQs6DH A.1936@TK2MSFTN GP12.phx.gbl...[color=darkred]
                      > > > William... I believe we need to clarify something. It isn't going to
                      > > > return FALSE because of the way he wrote it. It is going to return[/color][/color]
                      > FALSE[color=green][color=darkred]
                      > > > because it ends up FALSE when no return value has been set.
                      > > >
                      > > >
                      > > > Woody... I don't think you want it to just "accidental ly" work. Test[/color][/color][/color]
                      it[color=blue][color=green][color=darkred]
                      > > > yourself but (as part of the test) have a function return an integer[/color][/color][/color]
                      so[color=blue][color=green]
                      > > you[color=darkred]
                      > > > can get more than 2 return values.
                      > > >
                      > > > Unless I'm greatly mistaken it works as follows:
                      > > >
                      > > > You want two return statements in your code: Return True in the Try[/color][/color]
                      > block[color=green][color=darkred]
                      > > > and Return False after the End Try. With no return in Catch or[/color][/color][/color]
                      Finally.[color=blue][color=green][color=darkred]
                      > > >
                      > > > If there is no error it will return the value in the Try block (TRUE)[/color][/color][/color]
                      if[color=blue][color=green][color=darkred]
                      > > > there is an error the Catch block executes, the the Finally and you[/color][/color][/color]
                      will[color=blue][color=green][color=darkred]
                      > > > return the value of the Return statement (FALSE).
                      > > >
                      > > > Interestingly even if you place a return statement in the Catch block[/color][/color][/color]
                      it[color=blue][color=green][color=darkred]
                      > > > will not be returned. Either the Try return works or the final return[/color]
                      > > does.[color=darkred]
                      > > >
                      > > > Test it out...
                      > > > Tom
                      > > >
                      > > >
                      > > >
                      > > > "William Ryan eMVP" <bill@NoSp4m.de vbuzz.com> wrote in message
                      > > > news:uFrMFxr6DH A.2952@TK2MSFTN GP09.phx.gbl...
                      > > > > Finally will always be executed and yes, it will still return false[/color][/color]
                      > the[color=green][color=darkred]
                      > > > way
                      > > > > you wrote it.
                      > > > > "Woody Splawn" <woody@splawns. com> wrote in message
                      > > > > news:u7eMoqr6DH A.2300@TK2MSFTN GP10.phx.gbl...
                      > > > > > I have a try catch statement in a fucntion that is supposed to[/color][/color]
                      > return[color=green]
                      > > a[color=darkred]
                      > > > > true
                      > > > > > or a false
                      > > > > >
                      > > > > > My code looks like this:
                      > > > > >
                      > > > > > Try
                      > > > > > mySqlConnection .Open()
                      > > > > > Dim Da1 As New SqlDataAdapter( "Select JnlType,[/color][/color]
                      > Description[color=green][color=darkred]
                      > > > > from
                      > > > > > JnlType", mySqlConnection )
                      > > > > > Dim Ds As New DataSet("X")
                      > > > > > Da1.Fill(Ds)
                      > > > > > Catch
                      > > > > > MsgBox("There was a problem filling the Dataset for[/color][/color]
                      > Lookup[color=green][color=darkred]
                      > > > > table
                      > > > > > for JnlType")
                      > > > > > Return False
                      > > > > > Finally
                      > > > > > If mySqlConnection .State = ConnectionState .Open Then
                      > > > > > mySqlConnection .Close()
                      > > > > > End If
                      > > > > > End Try
                      > > > > >
                      > > > > > My question is this: Is the Try Catch code smart enough to run[/color][/color][/color]
                      the[color=blue][color=green][color=darkred]
                      > > > > Finally
                      > > > > > statment after the MsgBox and then return false or do I need to[/color][/color]
                      > write[color=green][color=darkred]
                      > > > the
                      > > > > > code some other way. This is what I want it to do but I am not[/color][/color][/color]
                      sure[color=blue]
                      > I[color=green][color=darkred]
                      > > > > have
                      > > > > > it written correctly.
                      > > > > >
                      > > > > >
                      > > > > >
                      > > > >
                      > > > >
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Peter Huang

                        #12
                        RE: Try Catch question

                        Hi Woody,

                        Thanks for posting in the community.

                        First of all, I would like to confirm my understanding of your issue.
                        From your description, I understand that you are confused by the
                        Try...Catch...F inally...End Try Block.
                        Have I fully understood you? If there is anything I misunderstood, please
                        feel free to let me know.

                        I think in the Try...Catch...F inally...End Try statement, if there is
                        Exceptionin the Try and The Exception was catched by Catch statement, the
                        Catch statement will be run.
                        And the Finally block will be called whether there is Exception in the Try
                        or not.

                        Here is my demo code you may run it to see if this will help you to
                        understand the Structured Exception Handling.
                        Module Module3
                        Public Function Func(ByVal isEx As Boolean) As Boolean
                        Try
                        Console.WriteLi ne("Try Block")
                        If isEx Then
                        Throw New Exception("Thro w Error")
                        End If
                        Return True
                        Catch ex As Exception
                        Console.WriteLi ne("Catch Block:" + ex.Message)
                        Return False
                        Finally
                        Console.WriteLi ne("Finally Block")
                        End Try
                        Console.WriteLi ne("This line of code will not be run")
                        End Function
                        Public Sub Main()
                        Console.WriteLi ne(Func(True))
                        Console.WriteLi ne()
                        Console.WriteLi ne(Func(False))
                        End Sub
                        End Module

                        Also you may take a look at the link below,
                        Walkthrough: Structured Exception Handling

                        vawlkWalkthroug hStructuredExce ptionHandling.a sp


                        Best regards,

                        Peter Huang
                        Microsoft Online Partner Support

                        Get Secure! - www.microsoft.com/security
                        This posting is provided "AS IS" with no warranties, and confers no rights.

                        Comment

                        • Cor

                          #13
                          Re: Try Catch question

                          Hi Woody, Tom, Bill

                          I do it totaly different from the rest and I think this is the best way (for
                          a dataadapter)

                          :-))

                          \\\
                          Try
                          Dim Da1 As New SqlDataAdapter _
                          ("Select JnlType, Description from JnlType",
                          mySqlConnection )
                          Dim Ds As New DataSet("X")
                          Da1.Fill(Ds)
                          Catch sqlExc As SqlException
                          MessageBox.Show (sqlExc.ToStrin g)
                          Return False
                          Catch ex As Exception
                          MessageBox.Show (ex.Message)
                          Return False
                          Finally
                          mySqlConnection .Close()
                          End Try
                          Return True ' Tom, I would have forgotten this if I had not looked at your
                          message.
                          ///



                          Comment

                          • Cor

                            #14
                            Re: Try Catch question

                            Hi

                            A correction Jon pointed me on.

                            And than it becomes in my opinon.
                            \\\with a single datatable to fill
                            Try
                            Dim Da1 As New SqlDataAdapter _
                            ("Select JnlType, Description from JnlType",
                            mySqlConnection )
                            Dim Ds As New DataSet("X")
                            Da1.Fill(Ds)
                            Catch sqlExc As SqlException
                            MessageBox.Show (sqlExc.ToStrin g)
                            Return False
                            Catch ex As Exception
                            MessageBox.Show (ex.Message)
                            Return False
                            End Try
                            Return true
                            ///
                            Or with a multiple datatable to fill
                            \\\
                            Try
                            mySQLConnection .open
                            Try
                            Dim Da1 As New SqlDataAdapter _
                            ("Select JnlType, Description from JnlType",
                            mySqlConnection )
                            Dim Ds As New DataSet("X")
                            Da1.Fill(Ds,"Ta ble1")
                            dim da.command = ("Select JnlType, Description from
                            JnlType2")
                            Da1.Fill(Ds,"Ta ble2")
                            Catch sqlExc As SqlException
                            MessageBox.Show (sqlExc.ToStrin g)
                            Return False
                            Catch ex As Exception
                            MessageBox.Show (ex.Message)
                            Return False
                            End Try
                            Catch ex As Exception
                            MessageBox.Show (ex.Message)
                            Return False
                            Finally
                            mySQLConnection .close
                            End Try
                            Return true
                            ///


                            Comment

                            Working...