C# exception question

  • Thread starter Thread starter Kevin Jackson
  • Start date Start date
K

Kevin Jackson

In the following code snippet, will the finally block be executed when the
throw is executed in the catch block???? I'm assuming it will.

catch (Exception e)
{
// if (ContextUtil.IsInTransaction)ContextUtil.SetAbort();
ReportError objError = new ReportError();

objError.PersistError(e,null,"DBWrite:executeStoredProcedureReturnXML","Conn
ectionString = " + m_sqlConnectionString + " : blnClearFlag=" + blnClearFlag
+ " : Stored Proc Name=" + storedProcedureName + " : Element Name=" +
elementName);
throw;
}
finally
{
cnConnection.Close();
cnConnection.Dispose();
blnClearFlag = true;
}
 
Kevin said:
In the following code snippet, will the finally block be executed when the
throw is executed in the catch block???? I'm assuming it will.

catch (Exception e)
{
// if (ContextUtil.IsInTransaction)ContextUtil.SetAbort();
ReportError objError = new ReportError();

objError.PersistError(e,null,"DBWrite:executeStoredProcedureReturnXML","Conn
ectionString = " + m_sqlConnectionString + " : blnClearFlag=" + blnClearFlag
+ " : Stored Proc Name=" + storedProcedureName + " : Element Name=" +
elementName);
throw;
}
finally
{
cnConnection.Close();
cnConnection.Dispose();
blnClearFlag = true;
}


Hi -

This has been taken from:
http://msdn.microsoft.com/library/d...en-us/cpguide/html/cpconusingfinallyblock.asp

"""
Some resource cleanup, such as closing a file, must always be executed even
if an exception is thrown. To accomplish this, you can use a finally block.
A finally block is always executed, regardless of whether an exception is
thrown.
"""

Good luck.
 
No, it will not.

Please check the difference between how these two samples work:

try
{
Console.WriteLine ("try");
throw (new Exception ("throw in try"));
}
catch (Exception e)
{
Console.WriteLine ("catch");
throw;
}
finally
{
Console.WriteLine ("finally");
}

and

try
{
Console.WriteLine ("try");
throw (new Exception ("throw in try"));
}
catch (Exception e)
{
Console.WriteLine ("catch");
}
finally
{
Console.WriteLine ("finally");
}

the "throw" in the catch block causes another exception to be thrown. The
finally block refers only to exceptions and handling the flow related to the
try block. The exception from the catch block would have to be handled on
the outside like:

try
{
Console.WriteLine ("try 2");
try
{
Console.WriteLine ("try");
throw (new Exception ("throw in try"));
}
catch (Exception e)
{
Console.WriteLine ("catch");
throw;
}
finally
{
Console.WriteLine ("finally");
}
}
catch
{
Console.WriteLine ("catch 2");
}


Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply
 
You example is misleading but helpful to prove how it really works.

Try this code, you will see that finally does indeed get called inside the
Dummy call. The only reason your first example doesn't run the finally
block is because there is no exception handler on the outside to catch the
throw therefore it throws a system exception.

Or am I missing something???

static void Main(string[] args)
{

//

// TODO: Add code to start application here

//

try

{

Console.WriteLine ("try main");


Dummy();

}

catch (Exception e)

{

Console.WriteLine ("catch main");

}

finally

{

Console.WriteLine ("finally main");

}

}

static void Dummy()

{

try

{

Console.WriteLine ("try dummy");

throw (new Exception ("throw in try dummy"));

}

catch (Exception e)

{

Console.WriteLine ("catch dummy");

throw;

}

finally

{

Console.WriteLine ("finally dummy");

}
 
You are right. I was mistaken but wrote a good example. Thanks for pointing it out.

I didn't realize finally needed another try around it to work if there was the throw in the catch coupled with the finally.
 
Thanks for your help... It all makes sense now!

Kevin

message You are right. I was mistaken but wrote a good example. Thanks for pointing
it out.

I didn't realize finally needed another try around it to work if there was
the throw in the catch coupled with the finally.
 
Back
Top