469,889 Members | 1,233 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

C# exception question

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:executeStore dProcedureReturnXML","Conn
ectionString = " + m_sqlConnectionString + " : blnClearFlag=" + blnClearFlag
+ " : Stored Proc Name=" + storedProcedureName + " : Element Name=" +
elementName);
throw;
}
finally
{
cnConnection.Close();
cnConnection.Dispose();
blnClearFlag = true;
}
Nov 15 '05 #1
5 1676
Kevin Jackson wrote:
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:executeStore dProcedureReturnXML","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/de...nallyblock.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.

--
chris
Nov 15 '05 #2
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:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

"Kevin Jackson" <so*****@covad.net> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
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:executeStore dProcedureReturnXML","Conn ectionString = " + m_sqlConnectionString + " : blnClearFlag=" + blnClearFlag + " : Stored Proc Name=" + storedProcedureName + " : Element Name=" +
elementName);
throw;
}
finally
{
cnConnection.Close();
cnConnection.Dispose();
blnClearFlag = true;
}


Nov 15 '05 #3
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");

}

"Cezary Nolewajka" <c.*********************@no-sp-am-eh-mail.com> wrote in
message news:%2******************@TK2MSFTNGP10.phx.gbl...
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:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

"Kevin Jackson" <so*****@covad.net> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
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:executeStore dProcedureReturnXML","Conn
ectionString = " + m_sqlConnectionString + " : blnClearFlag=" +

blnClearFlag
+ " : Stored Proc Name=" + storedProcedureName + " : Element Name=" +
elementName);
throw;
}
finally
{
cnConnection.Close();
cnConnection.Dispose();
blnClearFlag = true;
}

Nov 15 '05 #4
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.

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

Nov 15 '05 #5
Thanks for your help... It all makes sense now!

Kevin

"Cezary Nolewajka" <c.*********************@no-sp-am-eh-mail.com> wrote in
message news:uZ**************@TK2MSFTNGP11.phx.gbl...
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.

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gonçalo Rodrigues | last post: by
10 posts views Thread by tony | last post: by
3 posts views Thread by JohnDeHope3 | last post: by
5 posts views Thread by Bry | last post: by
132 posts views Thread by Zorro | last post: by
5 posts views Thread by Vijay | last post: by
2 posts views Thread by Bob Altman | last post: by
9 posts views Thread by =?Utf-8?B?UmFq?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.