Hi,
This is probably a simple question but I'm curious. What is the purpose of
the "finally" statement? There must be a good reason to use it that I'm just
not getting...
How is this:
method1
{
try
{
x;
}
catch
{
y;
}
finally
{
z;
}
}
different than:
method1
{
try
{
x;
}
catch
{
y;
}
z;
}
Thank you,
Joe 10 6100
Often, there will be return statements either in the Try or the Catch, or
both. Putting this code in the finally guarantees that it will be executed
in any case. Where as otherwise, you would have to put a copy of it in the
Try and Catch right before the return.
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message
news:6D******** *************** ***********@mic rosoft.com... Hi,
This is probably a simple question but I'm curious. What is the purpose of the "finally" statement? There must be a good reason to use it that I'm just not getting...
How is this:
method1 { try { x; } catch { y; } finally { z; } }
different than:
method1 { try { x; } catch { y; }
z; }
Thank you, Joe
To release objects that you might have in the try block. The following
article on MSDN probably best explains it: http://msdn.microsoft.com/library/de...ogether_PG.asp
--
Manohar Kamath
Editor, .netWire www.dotnetwire.com
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message
news:6D******** *************** ***********@mic rosoft.com... Hi,
This is probably a simple question but I'm curious. What is the purpose
of the "finally" statement? There must be a good reason to use it that I'm
just not getting...
How is this:
method1 { try { x; } catch { y; } finally { z; } }
different than:
method1 { try { x; } catch { y; }
z; }
Thank you, Joe
The answer is that in your second example, "z" is not guaranteed to be
executed, whereas in your first example, it is.
One caveat, though. Your specific examples were a bit unrealistic: it's
bad style to simply have a "catch" without saying what to catch, a
"catch-all" if you'll pardon the pun. So the way that your examples are
written, the only way that "z" will not be executed in the second
example is if "y" throws an exception (inside the catch block).
A more realistic case would be one in which the "catch" blocks catch
specific exceptions, in which case you need "finaly" in case some other
kind of exception that you're not catching occurs.
Sometimes people (like me) even use "finally" with no "catch" at all:
we want the exceptions to percolate up to the caller, but we have to be
sure to release some resource, reset some value, or change some setting
before leaving.
In my code, changing the cursor to an hourglass is a good example:
// Change the cursor to an hourglass
try
{
.... do a long, slow, complicated operation...
}
finally
{
// Change the cursor back to the default
}
I change the cursor back to a default in the finally block because I
want that to happen _even if_ the complex operation inside the "try"
blows up. In fact, the operation is complex enough that it has already
blown up on me several times for reasons beyond my control. If my
"Change the cursor back" code weren't within the "finally" then my
cursor would be stuck as an hourglass.
Hi Manohar,
Thanks for the reply. I read the article but I still don't see the
difference. Maybe if the example actually showed a resource being released
instead of a Console.WriteLi ne it would be easier to understand...
Joe
"Manohar Kamath" wrote: To release objects that you might have in the try block. The following article on MSDN probably best explains it:
http://msdn.microsoft.com/library/de...ogether_PG.asp
-- Manohar Kamath Editor, .netWire www.dotnetwire.com
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message news:6D******** *************** ***********@mic rosoft.com... Hi,
This is probably a simple question but I'm curious. What is the purpose of the "finally" statement? There must be a good reason to use it that I'm just not getting...
How is this:
method1 { try { x; } catch { y; } finally { z; } }
different than:
method1 { try { x; } catch { y; }
z; }
Thank you, Joe
Hi Marina,
That actually makes some sense to me. I guess if no returns were coded in
the try or catch blocks then the second way I showed would work the same as
the first then?
Thanks again,
Joe
"Marina" wrote: Often, there will be return statements either in the Try or the Catch, or both. Putting this code in the finally guarantees that it will be executed in any case. Where as otherwise, you would have to put a copy of it in the Try and Catch right before the return.
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message news:6D******** *************** ***********@mic rosoft.com... Hi,
This is probably a simple question but I'm curious. What is the purpose of the "finally" statement? There must be a good reason to use it that I'm just not getting...
How is this:
method1 { try { x; } catch { y; } finally { z; } }
different than:
method1 { try { x; } catch { y; }
z; }
Thank you, Joe
Hi Bruce,
That is a very good example and explanation.
Thanks for the clarification,
Joe
"Bruce Wood" wrote: The answer is that in your second example, "z" is not guaranteed to be executed, whereas in your first example, it is.
One caveat, though. Your specific examples were a bit unrealistic: it's bad style to simply have a "catch" without saying what to catch, a "catch-all" if you'll pardon the pun. So the way that your examples are written, the only way that "z" will not be executed in the second example is if "y" throws an exception (inside the catch block).
A more realistic case would be one in which the "catch" blocks catch specific exceptions, in which case you need "finaly" in case some other kind of exception that you're not catching occurs.
Sometimes people (like me) even use "finally" with no "catch" at all: we want the exceptions to percolate up to the caller, but we have to be sure to release some resource, reset some value, or change some setting before leaving.
In my code, changing the cursor to an hourglass is a good example:
// Change the cursor to an hourglass try { .... do a long, slow, complicated operation... } finally { // Change the cursor back to the default }
I change the cursor back to a default in the finally block because I want that to happen _even if_ the complex operation inside the "try" blows up. In fact, the operation is complex enough that it has already blown up on me several times for reasons beyond my control. If my "Change the cursor back" code weren't within the "finally" then my cursor would be stuck as an hourglass.
Joe...
try
{
OpenToiletLid() ;
Flush();
}
catch(OverflowE xception e)
{
CallPlumber();
}
finally
{
CloseToiletLid( );
}
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
I think my wife would like that if I could find a way to implement the
finally at home :-)
"Jeff Louie" <je********@yah oo.com> wrote in message
news:O4******** ******@TK2MSFTN GP09.phx.gbl... Joe... try { OpenToiletLid() ; Flush(); } catch(OverflowE xception e) { CallPlumber(); } finally { CloseToiletLid( ); }
Regards, Jeff
*** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
Hello! I think my wife would like that if I could find a way to implement the finally at home :-)
LOL!
Another approach - ask your wife to implement ICareless :-)
--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jon Skeet [C# MVP] |
last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the
article on all the groups this is posted to.
I've finally managed to finish my article on multi-threading - at least
for the moment. I'd be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it's somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.
Feel free to mail...
|
by: Max Ivanov |
last post by:
Hi all!
When and where I should use try-except-finally statement? What is the
difference between:
--------
try:
A...
except:
B....
finally:
C...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |