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