Try, Catch, Finally question
-
I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:
SqlConnection conn = null;
SqlCommand cmd = null;try
{
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd != null)
cmd.Dispose();if (conn != null)
conn.Dispose();
}IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?
-
I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:
SqlConnection conn = null;
SqlCommand cmd = null;try
{
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd != null)
cmd.Dispose();if (conn != null)
conn.Dispose();
}IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?
The finally block will almost always execute - let all dispose methods remain in the finally block.
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial
-
I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:
SqlConnection conn = null;
SqlCommand cmd = null;try
{
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd != null)
cmd.Dispose();if (conn != null)
conn.Dispose();
}IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?
Yes.... code in finally block will always execute unconditionally (unless you shut down the PC by pulling the power cord ...)
-
I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:
SqlConnection conn = null;
SqlCommand cmd = null;try
{
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd != null)
cmd.Dispose();if (conn != null)
conn.Dispose();
}IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?
JD86 wrote:
make sure I dispose of my objects
In such a case, I prefer a
using
statement:using (IDbConnection conn = ...)
{
using (IDbCommand cmd = ...)
{
//do something here
}
}Rhe Dispose method of the object will be called when the using block is left - regardless of the reason for leaving.
-
JD86 wrote:
make sure I dispose of my objects
In such a case, I prefer a
using
statement:using (IDbConnection conn = ...)
{
using (IDbCommand cmd = ...)
{
//do something here
}
}Rhe Dispose method of the object will be called when the using block is left - regardless of the reason for leaving.
-
The finally block will almost always execute - let all dispose methods remain in the finally block.
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial
-
I don't really like the using method. No way to really catch errors unless you do another try, catch within the using statement and then that is just redundant. I just stick to try, catch, finally and dispose of everything myself.
So use finally, it will do exactly you want. Just to reinforce, yes, it runs all the time, in good or bad cases (exceptions).