473,396 Members | 1,913 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,396 software developers and data experts.

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 1858
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gonçalo Rodrigues | last post by:
Hi, For error processing I found convenient maintaining a dictionary where the keys are exception *classes* and the values are callables. Of course, for this to work, exception classes have to...
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
12
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
10
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
2
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.