[dba-VB] try catch finally

Michael Maddison michael at ddisolutions.com.au
Fri Jul 23 00:25:49 CDT 2010


Coding in Outlook so watch out :-)

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 connection
--> if connection open
{
   open a new reader
-->If reader.hasrows();
	{
    		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
{
-->I rarely use this
}

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