Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2007-03-28
3
270 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
ID: 18808673
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
ID: 18808718
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
ID: 18809191
But, please never do it in a real application.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 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