[dba-VB] try catch finally

Doris Manning mikedorism at verizon.net
Fri Jul 23 13:13:32 CDT 2010


John,

You are correct that you do have to dimension the connection and reader
before the try.  The Finally block executes whether an error is encountered
or not.

A good developer always makes sure the garbage got taken care of on the way
out the door.  The suggestion of checking that the connection did get opened
before you execute the rest of the code isn't a bad idea.  However, things
can still go wrong once the reader/connection are opened and I've personally
seen cases where the server-side still sees the connection as open even
though the client-side thinks it is gone.

Instead of handling the error and then throwing the exception up to the
calling block, why not use Functions instead of Procedures and pass back a
Boolean indicating whether the operation was successful.

Doris Manning
Sr. Developer & Database Administrator
Hargrove Inc.

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Friday, July 23, 2010 1:12 AM
To: VBA
Subject: [dba-VB] try catch finally

OK, so I am trying to learn exception handling.  One of the things I am
reading is to make heavy use 
of finally to do cleanup.  Fine, except I am ending up with scope issues.
for example:

Try
{
    set a new connection
    open a new reader
    Do something with the reader
    close the reader
    close the connection
}
catch(sqlexception)
{
    handle the sql errors
    throw;
}
catch (exception)
{
    handle the nonsql errors
    throw;
}
finally
{
}

Logically the close of the reader and connection should go in the finally,
with the catch catching 
any issues actually opening the connection or reader.  That doesn't work
however because the 
connection and reader are not in scope in the finally block.

It appears that the dimensioning of the connection and reader have to go
before the try in order for 
the finally to see the scope?

I assume that the finally block executes after the throw?

What happens to the execution thread on a throw?  Does the thread execute
code up in any sink up 
above?  Does execution return back into this block of code after the code up
in the the parent sink 
finishes executing?

I am so confused.

;)


-- 
John W. Colby
www.ColbyConsulting.com
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com






More information about the dba-VB mailing list