473,327 Members | 1,952 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,327 software developers and data experts.

return statements and try catch finally blocks

Hi all
If inside a try-catch-finally block, return statements are
used, will the finally block get executed or not? Your
replies are very important to me. Thanks a lot.
Nov 15 '05 #1
5 30423
finally will always execute .
"C# beginner" <an*******@discussions.microsoft.com> wrote in message
news:18****************************@phx.gbl...
Hi all
If inside a try-catch-finally block, return statements are
used, will the finally block get executed or not? Your
replies are very important to me. Thanks a lot.

Nov 15 '05 #2
yes, that's precisely their purpose. If not, then they would be pretty much
useless.

If you want to really learn the inner workings...

Call a class that has a try/catch around it. From that class, call another
class with a method with no exception handler. After that, do it again, but
in the last class, just use Try/Finally. Put a messagebox in the top level
handler and put a message box in the finally. I think you'll find the
sequence interesting.

HTH,

Bill
"C# beginner" <an*******@discussions.microsoft.com> wrote in message
news:18****************************@phx.gbl...
Hi all
If inside a try-catch-finally block, return statements are
used, will the finally block get executed or not? Your
replies are very important to me. Thanks a lot.

Nov 15 '05 #3
Just as a side thought. I think it would have taken less time to test it
then to post this message to newgroup.

C# beginner wrote:
Hi all
If inside a try-catch-finally block, return statements are
used, will the finally block get executed or not? Your
replies are very important to me. Thanks a lot.

Nov 15 '05 #4
Leon Lambert <la******@inil.com> wrote:
Just as a side thought. I think it would have taken less time to test it
then to post this message to newgroup.


While that's true and I certainly encourage testing, there *is* a point
to asking questions even if you've tested them and think you know the
answer. Just because something works once doesn't mean it's guaranteed
to work. For instance, will the result of the following program always
be 10000? It has been every time I've run it, but it's far from
guaranteed to be.

using System;
using System.Threading;

class Test
{
static int count;

static void Main()
{
ThreadStart starter = new ThreadStart(IncrementCount100Times);
Thread[] threads = new Thread[100];
for (int i=0; i < threads.Length; i++)
{
threads[i] = new Thread(starter);
threads[i].Start();
}
for (int i=0; i < threads.Length; i++)
{
threads[i].Join();
}
Console.WriteLine (count);
}

static void IncrementCount100Times()
{
for (int i=0; i < 100; i++)
{
count++;
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Very true. I just get a bit annoyed when it seems that some peoples
first instict is to post to usenet rather than do a google search or try
a bit of coding themselves. I personally think people learn more by
doing, trying, and sometimes failing then just asking someone else how
to do something. They can then ask more experienced people if the answer
they found is proper or not afterwards.

I have limited work time to peruse up to 5 usenet groups everyday so
sometimes I get a bit annoyed.
Jon Skeet [C# MVP] wrote:
Leon Lambert <la******@inil.com> wrote:
Just as a side thought. I think it would have taken less time to test it
then to post this message to newgroup.

While that's true and I certainly encourage testing, there *is* a point
to asking questions even if you've tested them and think you know the
answer. Just because something works once doesn't mean it's guaranteed
to work. For instance, will the result of the following program always
be 10000? It has been every time I've run it, but it's far from
guaranteed to be.

using System;
using System.Threading;

class Test
{
static int count;

static void Main()
{
ThreadStart starter = new ThreadStart(IncrementCount100Times);
Thread[] threads = new Thread[100];
for (int i=0; i < threads.Length; i++)
{
threads[i] = new Thread(starter);
threads[i].Start();
}
for (int i=0; i < threads.Length; i++)
{
threads[i].Join();
}
Console.WriteLine (count);
}

static void IncrementCount100Times()
{
for (int i=0; i < 100; i++)
{
count++;
}
}
}

Nov 15 '05 #6

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

Similar topics

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()
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.