Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Fri Jul 23 13:05:08 CDT 2010
Hi John,
Yes, you can dimension connection and reader object variables outside the
try/catch block - something like it is done by the code in P.S. of this
posting.
If you have unhandled exception in your worker thread then it completes
abnormally but your main thread continues running OK, still you'd better
have all your worker thread exceptions handled gracefully and returning some
failing code to the main thread in the case of runtime errors...
Throw call issued and executed in catch block does immediately return
control/"bubles" to the nearest try/catch block in the call stack, and if
there is no any then worker or main thread crash...
HTH,
-- Shamil
P.S.
static void Main(string[] args)
{
try
{
tryCatchFinally();
}
catch (Exception ex)
{
System.Console.WriteLine("Runtime.Error: " + ex.Message);
}
}
public static void tryCatchFinally()
{
string serverName = @"MyServer\MySQLServer";
string databaseName = "MyDb";
SqlConnection cnn = null;
SqlCommand cmd = null;
SqlDataReader rdr = null;
string connectionString = string.Format(
@"Data Source={0};Initial Catalog={1};Integrated Security=True",
serverName, databaseName);
try
{
string cmdText = "select * from [Users]";
cnn = new SqlConnection(connectionString);
cnn.Open();
cmd = new SqlCommand(cmdText, cnn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
System.Console.WriteLine("{0} {1} {2}",
rdr[0], rdr[1], rdr[2]);
}
}
catch (SqlException ex)
{
if (cnn != null)
System.Console.WriteLine("tryCatchFinally.SQL: {0} {1} {2}",
cnn.WorkstationId, cnn.State, ex.Message);
throw;
}
catch (Exception ex)
{
if (cnn != null)
System.Console.WriteLine("tryCatchFinally.Runtime: {0} {1} {2}",
cnn.WorkstationId, cnn.State, ex.Message);
throw;
}
finally
{
if (rdr != null) rdr.Close();
if (cnn != null) cnn.Close();
}
}
-----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 9: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