"Angel Saenz-Badillos[MS]" <
[email protected]> a écrit dans le
message de This definitely gets my vote <g> when I think of all the trouble we could
have saved if we had written the docs in this fashion...
Unfortunately VB.net does not support the using construct and it still
requires try finally blocks.
Just one comment:
I would let it propagate upwards, until it reaches a
"strategic" point in your code (or in the framework)
Please not in the framework!
Why not? In a WinForms app, if you do not catch exceptions in your event
handlers, the exceptions ends up being caught by the framework, which
triggers a ThreadException event. The default dialog box that the framework
displays is not the one I would like to see in a real application (I don't
want to give the option to kill the application, at least not in such a
visible way). So, the easiest way to handle all these exceptions it to let
the framework catch them and to associate a custom error dialog to the
ThreadException event. This way, you are sure that all exceptions will be
handled and reported in a consistent way.
There is one pitfall with this scheme, though: it does not work when the
application is run from the debugger, probably because the debugger
short-circuits the ThreadException event. This is a pain because the
application dies on the first exception when you run it from the debugger.
On the other hand, it works great when the application is run normally.
Bruno.
--
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no
rights.Please do not send email directly to this alias.
This alias is for newsgroup purposes only.
No, dr cannot be null. ExecuteReader will throw an exception if it
fails,
it
will not return null.
You can simplify all this and rewrite it with the "using" statement
because
all the objects that you manipulate implement the IDisposable interface.
Here is how I would rewrite this:
using (SqlConnection cndb = new SqlConnection(connstr))
{
cndb.Open();
using (SqlCommand cmd = new SqlCommand("exec mysp", cndb))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
// your stuff here
}
}
}
Also, unless you really want to log the exception at this level (you
should
return a status indicating that the operation failed), I would not catch
the
exception here? I would let it propagate upwards, until it reaches a
"strategic" point in your code (or in the framework) where it will be
logged
and where the execution will resume.
Bruno.
"RepStat" <
[email protected]> a écrit dans le message
de
If I have code such as
SqConnection cndb = new SqlConnection(connstr);
SqlDataReader dr = null;
try
{
cndb.Open();
SqlCommand cmd = new SqlCommand("exec mysp", cndb);
dr = cmd.ExecuteReader();
}
catch (System.Exception e1) {Debug.WriteLine(e1.Message);}
finally
{
if(dr != null) dr.Close();
cndb.Close();
}
what bugs me is that is there any possibility that dr would be
non-null,
but not open - when the finally block started - thus causing the line
if(dr != null) dr.Close();
to fail, which would cause the cndb.Close() to never occur, leaving
the
connection open?
This is in an ASP.NET page.
Surely I don't have to put another try...finally within the first
finally?
or is there some property I can use to check the state of it?
Thanks!
Any suggestions please...