return statements and try catch finally blocks

  • Thread starter Thread starter C# beginner
  • Start date Start date
C

C# beginner

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.
 
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
 
Just as a side thought. I think it would have taken less time to test it
then to post this message to newgroup.
 
Leon Lambert said:
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 = new Thread(starter);
threads.Start();
}
for (int i=0; i < threads.Length; i++)
{
threads.Join();
}
Console.WriteLine (count);
}

static void IncrementCount100Times()
{
for (int i=0; i < 100; i++)
{
count++;
}
}
}
 
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.

Leon Lambert said:
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 = new Thread(starter);
threads.Start();
}
for (int i=0; i < threads.Length; i++)
{
threads.Join();
}
Console.WriteLine (count);
}

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