Try Catch Finally Using

M

Mike

All the code samples I can find show using on its own or try... catch..
..finally on its own. Will the following cover me properly in terms of
handling errors and cleaning up?

string conns = "Provider=Microsoft.Jet.OleDb.4.0;Data
Source=|DataDirectory|Northwind.mdb";
try
{
using (OleDbConnection conn = new OleDbConnection(conns))
{
OleDbCommand cmd = new OleDbCommand("SELECT * From FOO", conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader();
//do stuff
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

Clearly Access and Response.Write are just being used for demo purposes...
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

In this sample, if there is an error in the code, the connection will
have Dispose called on it (personally, I like to place the command and
reader in using statements as well) when the scope is exited.

If the using statement is exited as the result of an exception, then the
catch block will be executed in this case.
 
M

Marc Gravell

Well, cmd and rd are disposable too, so you could be "using" them as
well:

using (OleDbConnection conn = new OleDbConnection(conns))
using (OleDbCommand cmd = new OleDbCommand("SELECT * From FOO", conn))
{ // just to highlight you don't need a new brace/indent each time...
conn.Open();
using(OleDbDataReader rd = cmd.ExecuteReader()) {
//do stuff
}
}

I'm assuming that you simply want to log the error to the response (as
it won't get re-thrown by default). Note that in 1.1 you can get
exceptions (from managed C++ for example) that aren't Exception based,
so your "catch" might not fire. In 2.0 such exceptions get
automatically wrapped for you by default, so this isn't a problem.

But other than than, looks fine...

Marc
 
C

Christof Nordiek

Mike said:
All the code samples I can find show using on its own or try... catch..
.finally on its own. Will the following cover me properly in terms of
handling errors and cleaning up?

string conns = "Provider=Microsoft.Jet.OleDb.4.0;Data
Source=|DataDirectory|Northwind.mdb";
try
{
using (OleDbConnection conn = new OleDbConnection(conns))
{
OleDbCommand cmd = new OleDbCommand("SELECT * From FOO", conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader();
//do stuff
}

The using statement ensures, that the resource (in this case, the
OleDbConnection) is disposed on exiting the block of the using statement.
This will work indepently of how the block is exited, wether by normal flow,
an exception or a jumpstatement or what else.
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

This block will handle any excption wich falls out of the try block. Because
it is a general catch block, no exception whatsoever occuring inside the try
block will exit the try-catch-statement.
It this is a good approach can be doubted ;-)

The error handling will occur directly after the disposing of the connection
(if it occured inside the using block).
Clearly Access and Response.Write are just being used for demo purposes...
I hope so ;-)

Does this make it clearer to you?

Christof
 
M

Mike

Thank you to everyone :)

Mike

Nicholas Paldino said:
Mike,

In this sample, if there is an error in the code, the connection will
have Dispose called on it (personally, I like to place the command and
reader in using statements as well) when the scope is exited.

If the using statement is exited as the result of an exception, then
the catch block will be executed in this case.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
All the code samples I can find show using on its own or try... catch..
.finally on its own. Will the following cover me properly in terms of
handling errors and cleaning up?

string conns = "Provider=Microsoft.Jet.OleDb.4.0;Data
Source=|DataDirectory|Northwind.mdb";
try
{
using (OleDbConnection conn = new OleDbConnection(conns))
{
OleDbCommand cmd = new OleDbCommand("SELECT * From FOO", conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader();
//do stuff
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

Clearly Access and Response.Write are just being used for demo
purposes...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top