PC Review


Reply
Thread Tools Rate Thread

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

 
 
Savvoulidis Iordanis
Guest
Posts: n/a
 
      19th Jul 2008
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
 
Reply With Quote
 
 
 
 
Peter Lykkegaard
Guest
Posts: n/a
 
      19th Jul 2008
"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

 
Reply With Quote
 
George
Guest
Posts: n/a
 
      19th Jul 2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "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


 
Reply With Quote
 
George
Guest
Posts: n/a
 
      19th Jul 2008
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" <(E-Mail Removed)> wrote
in message news:2838406D-6955-466D-A5AF-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Peter Lykkegaard
Guest
Posts: n/a
 
      19th Jul 2008
"George" skrev

> That is incorrrect..
>

Ok thanks for the correction

- Peter
 
Reply With Quote
 
Peter Lykkegaard
Guest
Posts: n/a
 
      19th Jul 2008
"George" skrev

> That is incorrrect..
>

Ok thanks for the correction

- Peter
 
Reply With Quote
 
Mark Stevens
Guest
Posts: n/a
 
      20th Jul 2008
On Sat, 19 Jul 2008 12:27:16 -0400, "George" <(E-Mail Removed)>
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".
 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      20th Jul 2008
Mark Stevens wrote:
> On Sat, 19 Jul 2008 12:27:16 -0400, "George" <(E-Mail Removed)>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
throw statement inside a catch block continually caught be same catchblock? Tracy Bannon Microsoft C# .NET 3 3rd Jan 2009 02:22 PM
try..catch catch the error only running inside of VS.NET Dragos Hilbert Microsoft C# .NET 2 11th Mar 2008 03:35 PM
Raiseevent inside catch block RJ Microsoft Dot NET Framework 3 5th Apr 2006 01:29 PM
Catch block is failing to catch exceptions when not run from MSDev CodeSlayer Microsoft C# .NET 2 16th Feb 2006 06:42 PM
Response.Redirect inside a Try Catch block Craig Microsoft Dot NET 2 15th Sep 2005 02:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:35 AM.