473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

CODING PRACTICE: Returning from function inside a TRY/CATCH block?

Is it right when placing the RETURN statement inside the TRY or inside the
CATCH statement, when there is a FINALLY clause? Especially when there is a
transaction going on, in the try/catch block?

I give you the following example to meka it more clear:
(I use Enterprise Library, but the same also applies without it)

public function f_SomeFunction(parm1,....) as integer
dim tr as DbTransaction

Using conn As DbConnection = db.CreateConnection()
conn.Open()

tr = conn.BeginTransaction()

try
...db action 1
...db action 2
...db action 3
tr.Commit()
return 1

catch ex as Exception
tr.Rollback()
Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex,
"POLICY_NAME_HERE")
If rethrow Then
Throw
End If

return -1

finally
conn.Close()
end try
end using
end function

Or instead, I should just set a flag variable rather than commiting/rolling
back (eg. b_ok to TRUE in the TRY clause or FALSE in the CATCH block) and
then check its value outside the TRY/CATCH block or the Using block and see
if I should Return 1 or -1?

I don't know if it's just a matter of programming preference or I could
sometime get unexpected behavior. Which is the common practice? Any help is
appreciated

TIA
Iordanis
Jul 19 '08 #1
8 2467
"Savvoulidis Iordanis" wrote
Is it right when placing the RETURN statement inside the TRY or inside the
CATCH statement, when there is a FINALLY clause?
When ever you have a return statement you are exiting your method
immediately and the connection is not closed

- Peter

Jul 19 '08 #2
That is incorrrect..

If you closing your connection inside of Finally block then even if you
exiting with Return Finally block will run and connection will close...

George.
"Peter Lykkegaard" <pl@teklogix.dkwrote in message
news:u2**************@TK2MSFTNGP06.phx.gbl...
"Savvoulidis Iordanis" wrote
>Is it right when placing the RETURN statement inside the TRY or inside
the
CATCH statement, when there is a FINALLY clause?
When ever you have a return statement you are exiting your method
immediately and the connection is not closed

- Peter
Jul 19 '08 #3
Your code is perfectly fine except a little confusing.
If you using "Using" statement then i do not see need to use try/finally
block. Use one or another. You do not need both. But in your case you want
to actually catch exception so kill the Using statement.
Just move tr = conn.BeginTransaction() into try statement.
So i would rewrite it as follow (I moved tr = conn.BeginTransaction()
and also checking on tr not beign Nothing befor doing tr.Rollback since
BeginTransaction can throw an error and tr will be Nothing

public function f_SomeFunction(parm1,....) as integer
dim tr as DbTransaction
Dim conn As DbConnection = db.CreateConnection()
conn.Open()
try
tr = conn.BeginTransaction()
...db action 1
...db action 2
...db action 3
tr.Commit()
return 1
catch ex as Exception
if( Not tr Is Nothing )
tr.Rollback()
Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex,
"POLICY_NAME_HERE")
If rethrow Then
Throw
End If
return -1
finally
conn.Close()
end try
end function

George.
"Savvoulidis Iordanis" <Sa*****************@discussions.microsoft.comwrot e
in message news:28**********************************@microsof t.com...
Is it right when placing the RETURN statement inside the TRY or inside the
CATCH statement, when there is a FINALLY clause? Especially when there is
a
transaction going on, in the try/catch block?

I give you the following example to meka it more clear:
(I use Enterprise Library, but the same also applies without it)

public function f_SomeFunction(parm1,....) as integer
dim tr as DbTransaction

Using conn As DbConnection = db.CreateConnection()
conn.Open()

tr = conn.BeginTransaction()

try
...db action 1
...db action 2
...db action 3
tr.Commit()
return 1

catch ex as Exception
tr.Rollback()
Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex,
"POLICY_NAME_HERE")
If rethrow Then
Throw
End If

return -1

finally
conn.Close()
end try
end using
end function

Or instead, I should just set a flag variable rather than
commiting/rolling
back (eg. b_ok to TRUE in the TRY clause or FALSE in the CATCH block) and
then check its value outside the TRY/CATCH block or the Using block and
see
if I should Return 1 or -1?

I don't know if it's just a matter of programming preference or I could
sometime get unexpected behavior. Which is the common practice? Any help
is
appreciated

TIA
Iordanis
Jul 19 '08 #4
"George" skrev
That is incorrrect..
Ok thanks for the correction :)

- Peter
Jul 19 '08 #5
"George" skrev
That is incorrrect..
Ok thanks for the correction :)

- Peter
Jul 19 '08 #6
On Sat, 19 Jul 2008 12:27:16 -0400, "George" <no*****@comcast.net>
wrote:
>If you using "Using" statement then i do not see need to use try/finally
block. Use one or another. You do not need both. But in your case you want
Correct me if I am wrong, but doesn't "using" tell the run-time system
to release any resources as soon as possible? This is a little
different from using the finally block to close the connection.

Also, in answer to the original poster, it is often considered good
style to only have one exit point from a method.

Regards
Mark
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)

This message is provided "as is".
Jul 20 '08 #7
"Mark Stevens" <ne***@nospam.nospamwrote in message
news:96********************************@4ax.com...
>If you using "Using" statement then i do not see need to use try/finally
block. Use one or another. You do not need both. But in your case you
want

Correct me if I am wrong, but doesn't "using" tell the run-time system
to release any resources as soon as possible? This is a little
different from using the finally block to close the connection.
It largely depends what the Try...Finally is used for. However, a Using
block and a Try...Finally construction are most certainly *not* mutually
exclusive. See here straight from the horse's mouth, as it were:
http://msdn.microsoft.com/en-us/libr...hh(VS.80).aspx
Also, in answer to the original poster, it is often considered good
style to only have one exit point from a method.
Yes, but only by people for whom such coding aesthetics are more important
than what the code actually does...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 20 '08 #8
Mark Stevens wrote:
On Sat, 19 Jul 2008 12:27:16 -0400, "George" <no*****@comcast.net>
wrote:
>If you using "Using" statement then i do not see need to use try/finally
block. Use one or another. You do not need both. But in your case you want

Correct me if I am wrong, but doesn't "using" tell the run-time system
to release any resources as soon as possible?
No, it disposes the object at the end of the using block, not sooner.
This is a little
different from using the finally block to close the connection.
No, a using block is just syntactic sugar for a try...finally, so there
is actually no difference at all.
Also, in answer to the original poster, it is often considered good
style to only have one exit point from a method.

Regards
Mark

--
Göran Andersson
_____
http://www.guffa.com
Jul 20 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
8
by: s.subbarayan | last post by:
Dear all, In one of our projects in a document about C coding standard it is stated as "Always check a pointer is NULL before calling free. Always set a free'd pointer to NULL to try to protect...
4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
4
by: Steve | last post by:
Hi All In VB6 I used to catch exceptions in a goto errtrap call then resume if I could handle the problem and continue within the routine or function I am not sure how to do this in VB 2005...
21
by: Jim Langston | last post by:
I'm sure this has been asked a few times, but I'm still not sure. I want to create a function to simplify getting a reference to a CMap in a map. This is what I do now in code: ...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
19
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.