471,319 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

try finally Error Handling without catch

Hi,

I underestand that we can use try finally without catch clause. Something
like this:
try
{
throw new Exception("Test")
}
finally
{
// close all files
}

What exactly is this?
Thank you,
Alan
Jan 18 '06 #1
5 7387
It ensures that when your code exits the try block, the code in the
finally is executed. You might use this for cleaning up external
resources (file handles etc.. )
A.M-SG wrote:
Hi,

I underestand that we can use try finally without catch clause. Something
like this:
try
{
throw new Exception("Test")
}
finally
{
// close all files
}

What exactly is this?
Thank you,
Alan

Jan 18 '06 #2
A.M-SG <al******@newsgroup.nospam> wrote:
I underestand that we can use try finally without catch clause. Something
like this:
try
{
throw new Exception("Test")
}
finally
{
// close all files
}

What exactly is this?


It means that the finally block is executed whatever happens, but any
exceptions thrown in the try block are still propagated up the stack
(rather than being caught).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 18 '06 #3
On Wed, 18 Jan 2006 16:48:13 -0500, "A.M-SG"
<al******@newsgroup.nospam> wrote:
Hi,

I underestand that we can use try finally without catch clause. Something
like this:
try
{
throw new Exception("Test")
}
finally
{
// close all files
}

What exactly is this?
Thank you,
Alan

I am not quite sure what you are asking. THe finally clause
guarantees that the code inside it will be executed, no matter how the
try block terminates. This includes if the try block throws an
exception.

Try:

try {
Console.WriteLine("Alpha");
throw new Exception("Test");
Console.WriteLine("Beta");
}
finally {
Console.WriteLine("Gamma");
}

and see which bits show on screen.

rossum


--

The ultimate truth is that there is no ultimate truth
Jan 18 '06 #4
If you are asking why you would use this, here is one snippet from my
code:

this._reactingToUser = true;
try
{
this.Model.PurchaseOrderNumber =
Convert.ToInt32(this._poNumber.Text);
... do more stuff to update the model ...
}
finally
{
this._reactingToUser = false;
}

This ensures that no matter what happens while I'm updating the model,
the _reactingToUser flag will always be set back to false at the end.

It's also handy for making sure that resources are released, as John
Murray pointed out, although there is the construct

using (...)
{
}

for that, too.

Jan 19 '06 #5
Thank you Jon. I was looking for what you said about call stack. Thanks for
help.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
A.M-SG <al******@newsgroup.nospam> wrote:
I underestand that we can use try finally without catch clause. Something
like this:
try
{
throw new Exception("Test")
}
finally
{
// close all files
}

What exactly is this?


It means that the finally block is executed whatever happens, but any
exceptions thrown in the try block are still propagated up the stack
(rather than being caught).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by KefX | last post: by
54 posts views Thread by Stefan Arentz | last post: by
16 posts views Thread by Chris | last post: by
9 posts views Thread by Andy Turner | last post: by
23 posts views Thread by VB Programmer | last post: by
26 posts views Thread by Grim Reaper | last post: by
32 posts views Thread by cj | last post: by
12 posts views Thread by David Lozzi | last post: by
reply views Thread by rosydwin | last post: by

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.