473,503 Members | 1,768 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 14684
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.google.c om...
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("Invalid 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.google.c om...
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****@games4dotnet.com> wrote in message news:<em**************@TK2MSFTNGP10.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("Invalid 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.google.c om...
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****@games4dotnet.com> wrote in message news:<em**************@TK2MSFTNGP10.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("Invalid 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.google.c om...
> 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
30251
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,...
5
2209
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
2318
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...
4
4332
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
3
2881
by: Steve | last post by:
I have some general catch clauses in my app as follows: try { } catch(Exception ex) { } try
13
3694
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
3
1833
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...
11
1973
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...
5
3013
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
23
2292
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
7199
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
7076
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
7323
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...
1
6984
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
7453
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...
0
5576
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,...
0
4670
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...
0
3162
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.