473,386 Members | 1,745 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,386 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 7475
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
54
by: Stefan Arentz | last post by:
I was just reading through some old articles in the 'Why not develop new language' thread and came across the finally debate. Everytime I mention 'finally' to C++ programmers I get almost...
16
by: Chris | last post by:
Hi, regarding exception-handling : why put code in a 'finally' block, and not just after a 'catch'-block --> Example using 'finally ' : try { OpenFile();
9
by: Andy Turner | last post by:
I was just pondering about what the Finally in an exception actually does. Well, I realise that it always gets called regardless of whether the exception is thrown or not, but isn't that the same...
16
by: Chris | last post by:
Hi, I am using the try catch finally block in many places in my code. When a create an sqldatareader dr and try to close it in my finally block I get the error: use of unsigned local variable....
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
26
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
12
by: David Lozzi | last post by:
Howdy, I ran into a very interesting issue and I'm curios as to how this is suppose to work. I am using Try...Catch...Finally statements for all database connectivity in my ASP.NET 2.0 web...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.