[dba-VB] try catch finally

jwcolby jwcolby at colbyconsulting.com
Fri Jul 23 00:11:31 CDT 2010


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



More information about the dba-VB mailing list