Solved

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

Posted on 2007-03-28
3
278 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Title # Comments Views Activity
C# Desktop Application 3 65
jquery + C#.net radiobuttonlist 3 45
Making exceptions for ValidationAttribute 2 24
Connect Gridview column to Textbox in C# 2 38
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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