Try Catch Quesiton Again

W

Woody Splawn

In a message yesterday titled Try Catch Question I got numerous responses.
Thank you to all. After all the smoke clears I guess the question is where
to place the Return True statement in the block of code below that is part
of a function. Should it be at the end of the try block, as dipicted below,
or should it be after the End Try Block? Or does it make a difference?

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
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
 
S

Scott M.

I would set up a Boolean that is set to true in the try and false in the
catch. Then in the finally, use an If statement to return the correct value
based on the Boolean.
 
B

Brian

It makes no difference. Just don't put it in the Finally block.

Try
Return True -----OK
Catch
..........
Finally
........
End Try

Or after End Try

Try
.........
Catch
..........
Finally
........
End Try

Return True -----------OK
 
W

Woody Splawn

Correct me if I'm wrong but I don't believe the finally section will allow
you to return a value.
 
C

CJ Taylor

I just declare a variable at the top like


dim retval as boolean

try
retval = true
catch
retval = false
finally
retval = true
end try

return retval
 
T

Tom Leylan

Perhaps the point got lost among all the responses.

You can add a return statement in the Try, in the Catch or after the End
Try. You cannot place one in the Finally. As you have it written it will
work. It looks a little "odd" however as we are normally led to believe
that Return False would return immediately and in this case it does not, it
returns that value after the Finally block. It also would (I believe) not
execute any code after the End Try. So it just reads poorly.

But rather than introduce an extra Boolean variable to contain the "response
value" simply assign the name of the Function.

FunctionName = True
or
FunctionName = False

in exactly the same place as you currently have your Return statements.
Then if there is no error the try block executes, sets the value and the
lines of code (if there were any) after the End Try runs and the function
exits.

If an error does occur the remainder of the code in the Try block is passed
over, the Catch block handles the error, it assigns the return value then
the code in the Finally block executes (if there is a Finally block) and
then any code after the End Try would execute (if there was any) and the
function exits.

The function returns from the same spot in either case but with a different
value set.

Tom
 
S

Scott M.

Oops, yes you are right. Here's an alternative...

Public Function test() As Boolean
Dim x As Boolean
Try
x = True
Catch
x = False
End Try

Return x
End Function

Note that this doesn't use a finally at all. You don't need to have a
funally in your try blocks. You can just place code after the end try and
it serves the same purpose.
 
C

CJ Taylor

I think you missed the point dude...

I wasn't dictiating what would run and what wouldn't... just showing how I
did it so you didn't have multiple return statements, that and if you wanted
to do some post processing after your try catch you couldn't with the return
statement where it was, you wouldn't be able to. I just ran a similar
example and as soon as I hit my return line it drops me out of the function.

Good luck,
CJ
 
C

Chris Dunaway

But your code *always* reutrns true. I would not set the return value in
the finally block since it always gets executed. But I understand your
point about not having multiple return statements.
 
P

Peter Huang

Hi Woody,

Thanks for posting in the community.

As I said in my reply to your post in another thread in this newsgroup, the
finally block will be excuted even if you have return statement in try or
catch block, the finally block is used to do some cleaning stuff whether or
not the code in the try block has been executed successfully.
So your code is just OK, also if you wants to prevent using many return
statement, to declare a boolean before the try is also a good idea, then
you can return the boolean variable after the try..end try block.

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.
 
T

Tom Leylan

Peter,

Would you agree that there is no need to define an additional boolean
variable since every function has a return variable available to it that
automatically has the correct datatype? Is there some reason not to use the
Function "name" as the return variable?

While you are here could you take a look at the "EnableVisualStyles" thread?
 
P

Peter Huang

Hi Tom,

Hi Tom,

Thanks for posting in the community.

I think this is the personal preference.
Module Module3
Public Function Func(ByVal isEx As Boolean) As Boolean
Try
Console.WriteLine("Try Block")
If isEx Then
Throw New Exception("Throw Error")
End If
Func = True
Catch ex As Exception
Console.WriteLine("Catch Block:" + ex.Message)
Func = False
Finally
Console.WriteLine("Finally Block")
End Try
Console.WriteLine("This line of code will not be run")
End Function
Public Sub Main()
Console.WriteLine(Func(True))
Console.WriteLine()
Console.WriteLine(Func(False))
End Sub
End Module

If you did not use the return statement, then the code below will be
executed.
Console.WriteLine("This line of code will not be run")
While
Module Module3
Public Function Func(ByVal isEx As Boolean) As Boolean
Try
Console.WriteLine("Try Block")
If isEx Then
Throw New Exception("Throw Error")
End If
Return True
Catch ex As Exception
Console.WriteLine("Catch Block:" + ex.Message)
Return False
Finally
Console.WriteLine("Finally Block")
End Try
Console.WriteLine("This line of code will not be run")
End Function
Public Sub Main()
Console.WriteLine(Func(True))
Console.WriteLine()
Console.WriteLine(Func(False))
End Sub
End Module

the code line will not be run.
Console.WriteLine("This line of code will not be run")

As for "EnableVisualStyles" thread, I am researching it.

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.
 
B

Brian

Peter you are wrong. The last line WILL always run if you use the function
name instead of using the Return.

Public Function Func(ByVal isEx As Boolean) As Boolean
Try
Console.WriteLine("Try Block")
If isEx Then
Throw New Exception("Throw Error")
End If
Func = True
Catch ex As Exception
Console.WriteLine("Catch Block:" + ex.Message)
Func = False
Finally
Console.WriteLine("Finally Block")
End Try
Console.WriteLine("This line of code will not be
run")<-----------------WILL ALWAYS RUN!!!!!!!!!!!!!!!!!!
End Function
Public Sub Main()
Console.WriteLine(Func(True))
Console.WriteLine()
Console.WriteLine(Func(False))
End Sub
End Module
 
J

Jay B. Harlow [MVP - Outlook]

Brian,
Which is one of the major reasons I avoid setting the function name to the
return value!

Its harder to "follow" where the program is going to go.

Jay
 
C

Cor

Hi Brian and Jay B.

I have the same idea about it.

In every language I have seen/used untill now.

I did not wanted to go in the discussion, but now it becomes such a long
thread.

I stay with
\\\
Try
catch
return false (I think normaly in such a routine not false but the correction
parameters)
finally
End Try
return true (or OK)
///
Cor
 
S

Scott M.

My 2 cents:

"Return" is more consistent with the OO programming paradigm of most other
languages, so I like to use that. Especially since I can still accomplish
what I want by re-working the code a little.
 
T

Tom Leylan

Jay,

I've got to drop out of the thread because I suspect it's going to
degenerate into everybody posting what they do :) but I will point out that
lots of people (including myself) have always set return variables and
exited from a single spot at the end of the function. Those who choose that
alternative (generally speaking) find it harder to follow code which can
exit from any number of places.

Everybody likes "easier" but curiously what some people find easier differs.

My recommendation (lost somewhere in the ether at this point) was not to use
the function name as a return variable "except" when somebody mentioned "you
can add a boolean variable." If one is going to add another boolean then
the function name (already defined) is at least no less understandable. In
other languages (most notably Clipper) I always named my return variable
"Result" regardless of it's type and I could count on Return Result at the
end of the function to return the proper value.

Well I think that's it for me on this subject. I'm recommending that
developers choose a system and stick with it (at least through an
application) if anything is hard to read it is app code that changes coding
styles several times.

Tom
 
G

Guest

Hi Woody,



Why Retun any value at all?

mySqlConnection.Open() ' Move open here and let let upstream
handle the error
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 Forget this
Catch e as Exception
throw new ApplicationException("There was a problem filling the
Dataset for Lookup table for JnlType")
'MsgBox("There was a problem filling the Dataset for Lookup
table for JnlType")
'Return False
Finally
mySqlConnection.Close() 'it wll never get here if it is not open
'If mySqlConnection.State = ConnectionState.Open Then
' mySqlConnection.Close()
'End If
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top