Solved

Try/Catch/Finally question. Somewhat short on time.

Posted on 2007-03-28
3
249 Views
Last Modified: 2010-04-16
just a quick question about   Try/Catch/Finally   blocks.

Lets say I have a bool function I'm writing.

try
{
   //some code
}
catch (Exception)
{
   return false;
}
finally
{
  //some code
}
return true;


if an exception occurs in the try block, will the finally block still execute? or does the return statement immediately leave the function?
0
Comment
Question by:raw_enha
3 Comments
 
LVL 2

Accepted Solution

by:
empoalp earned 500 total points
Comment Utility
Finally code is executed.

You can check it withs this code:

    class Program
    {
        static void Main(string[] args)
        {
            func();
            Console.Read();
        }

        public static bool func()
        {
            try
            {
                Console.WriteLine("try");
                Int32.Parse("jjjj");
            }
            catch (Exception)
            {
                Console.WriteLine("catch");
                return false;
            }
            finally
            {
                Console.WriteLine("finally");
            }
            return true;
        }
    }
0
 
LVL 15

Expert Comment

by:angus_young_acdc
Comment Utility
Yes your finally is executed, however you wouldnt loop back to re-do what failed in the try block.  

It is really just for catching and dealing with your exceptions
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
But, please never do it in a real application.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now