Solved

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

Posted on 2007-03-28
3
283 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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