473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Try Catch Finally Using

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...

Nov 16 '07 #1
4 4157
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]
- mv*@spam.guard.caspershouse.com

"Mike" <bl***@blank.blankwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
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...
Nov 16 '07 #2
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
Nov 16 '07 #3
"Mike" <bl***@blank.blankschrieb im Newsbeitrag
news:ud**************@TK2MSFTNGP06.phx.gbl...
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

Nov 16 '07 #4
Thank you to everyone :-)

Mike

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:BD**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Mike" <bl***@blank.blankwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
>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...

Nov 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tilfried Weissenberger | last post by:
Hi, I am a bit confused as to what the FINALLY block is meant for. What's the difference between: this.Cursor = Cursors.WaitCursor; try { //do some stuff } catch { //handle exception }...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
13
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
28
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " ...
12
by: David Lozzi | last post by:
Howdy, I ran into a very interesting issue and I'm curios as to how this is suppose to work. I am using Try...Catch...Finally statements for all database connectivity in my ASP.NET 2.0 web...
8
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
Is it right when placing the RETURN statement inside the TRY or inside the CATCH statement, when there is a FINALLY clause? Especially when there is a transaction going on, in the try/catch block?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.