473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

try, catch, finally- whats the point of finally?

It seems to me if you have a function like this

public void blah()

try
{
blah blah;
}

catch
{
blah de blah;
}

#code that executes whether there is an error or not#

return;

Then the code at the end will execute just as if it were in a finally
block. If that is the case then I don't know what the point of a
finally block is. Couls someone clear that up for me?

-Alan
Nov 16 '05 #1
7 14690
finally is a guarantee that the code will get run. Take this example:

try {

"Alan Paulk" <ap****@141.com > wrote in message
news:60******** *************** ***@posting.goo gle.com...
It seems to me if you have a function like this

public void blah()

try
{
blah blah;
}

catch
{
blah de blah;
}

#code that executes whether there is an error or not#

return;

Then the code at the end will execute just as if it were in a finally
block. If that is the case then I don't know what the point of a
finally block is. Couls someone clear that up for me?

-Alan

Nov 16 '05 #2
Darnit, sorry, the Ctrl+Enter bug hit again.

Take this code:

try {
// Throw your exception
} finally {
// Run your clean-up here
}

The exception will bubble up the call stack and get caught by
another catch block if one exists, but your finally code will still
get run. If you placed the code outside of the finally block, then
execution would stop at the exception and be thrown up the stack
to be caught. Let's take a resource allocation example.

try {
// My library accesses a stream
// Some error happens and I need to throw an exception for the user.
throw new Exception("Inva lid data was in the stream");
} finally {
// Close the stream here
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Alan Paulk" <ap****@141.com > wrote in message
news:60******** *************** ***@posting.goo gle.com...
It seems to me if you have a function like this

public void blah()

try
{
blah blah;
}

catch
{
blah de blah;
}

#code that executes whether there is an error or not#

return;

Then the code at the end will execute just as if it were in a finally
block. If that is the case then I don't know what the point of a
finally block is. Couls someone clear that up for me?

-Alan

Nov 16 '05 #3
Here's some good readings on it
http://www.jaggersoft.com/pubs/Excep...ngInCSharp.htm

--
hth
Hirantha
Please reply only to the newsgroups.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Also, code in a catch block gets called *only* if an exception is thrown. Code in a finally block gets called if an exception is thrown or if an exception is *not* thrown.
Nov 16 '05 #5
"Justin Rogers" <Ju****@games4d otnet.com> wrote in message news:<em******* *******@TK2MSFT NGP10.phx.gbl>. ..
Darnit, sorry, the Ctrl+Enter bug hit again.

Take this code:

try {
// Throw your exception
} finally {
// Run your clean-up here
}

The exception will bubble up the call stack and get caught by
another catch block if one exists, but your finally code will still
get run. If you placed the code outside of the finally block, then
execution would stop at the exception and be thrown up the stack
to be caught. Let's take a resource allocation example.

try {
// My library accesses a stream
// Some error happens and I need to throw an exception for the user.
throw new Exception("Inva lid data was in the stream");
} finally {
// Close the stream here
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Alan Paulk" <ap****@141.com > wrote in message
news:60******** *************** ***@posting.goo gle.com...
It seems to me if you have a function like this

public void blah()

try
{
blah blah;
}

catch
{
blah de blah;
}

#code that executes whether there is an error or not#

return;

Then the code at the end will execute just as if it were in a finally
block. If that is the case then I don't know what the point of a
finally block is. Couls someone clear that up for me?

-Alan


Thanks for the help!
-Alan
Nov 16 '05 #6
On 4 May 2004 02:54:47 -0700, ap****@141.com (Alan Paulk) wrote:
"Justin Rogers" <Ju****@games4d otnet.com> wrote in message news:<em******* *******@TK2MSFT NGP10.phx.gbl>. ..
Darnit, sorry, the Ctrl+Enter bug hit again.

Take this code:

try {
// Throw your exception
} finally {
// Run your clean-up here
}

The exception will bubble up the call stack and get caught by
another catch block if one exists, but your finally code will still
get run. If you placed the code outside of the finally block, then
execution would stop at the exception and be thrown up the stack
to be caught. Let's take a resource allocation example.

try {
// My library accesses a stream
// Some error happens and I need to throw an exception for the user.
throw new Exception("Inva lid data was in the stream");
} finally {
// Close the stream here
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Alan Paulk" <ap****@141.com > wrote in message
news:60******** *************** ***@posting.goo gle.com...
> It seems to me if you have a function like this
>
> public void blah()
>
> try
> {
> blah blah;
> }
>
> catch
> {
> blah de blah;
> }
>
> #code that executes whether there is an error or not#
>
> return;
>
> Then the code at the end will execute just as if it were in a finally
> block. If that is the case then I don't know what the point of a
> finally block is. Couls someone clear that up for me?
>
> -Alan


Thanks for the help!
-Alan


Most C++ programmers (including myself!) think the finally clause is
an absolute godsend!

Nov 16 '05 #7
(lots of snip)

Most C++ programmers (including myself!) think the finally clause is
an absolute godsend!


As far as I'm aware the reason that Stroustrup didn't put finally into C++
is because there is a much better idiom in that language: destruction of
stack-based objects.

C# (and .NET), which don't support deterministic finalization, needed an
alternative method of safely releasing resources etc.

Stu
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
30265
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program, it quitted with core dumped. %test
5
2213
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
24
2321
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no virtual funcitons have no rtti) That is, there is no way to do something like: try{ funct_from_3rd_party(); } catch(...){ std:err <<...
4
4334
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
3
2887
by: Steve | last post by:
I have some general catch clauses in my app as follows: try { } catch(Exception ex) { } try
13
3697
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
3
1838
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call a mehtod called ExceptionGenerator that will generate a run-time exception. The exception is caught in GenericGridEvent, but it isn't caught in...
11
1981
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be more specific. I can't find any property of the exception object that tells me WHICH one it is. TIA,
5
3020
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
23
2301
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7786
tracyyun
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...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.