开发者

How to stop finally block from execute in C#

开发者 https://www.devze.com 2023-03-26 09:56 出处:网络
I have created a try block.Inside that try block again try block and it\'s finally block.I closed that outside try block.followed by finally block.Suppose I don\'t want to execute the finally block,fo

I have created a try block.Inside that try block again try block and it's finally block.I closed that outside try block.followed by finally block.Suppose I don't want to execute the finally block,for that I wrote Environment.Exit(0).Does there is any other method that stop finally block from execution.return 0;(that is in java).Please let me know.

public static int Main()
        {
            try
            {
                Console.WriteLine("First try block");

                try
  开发者_C百科              {
                    Console.WriteLine("Second try block");
                    //exit(0);
                    Environment.Exit(0);
                    //return 0;
                }

                finally
                {
                    Console.WriteLine("Finally block of inner try");
                }
            }

            finally
            {
                Console.WriteLine("Finally block of second try");
            }


You can use Environment.FailFast() to exit the application immediately, while skipping finally and finalizers.

http://msdn.microsoft.com/en-us/library/ms131100.aspx


From the MSDN -

The statements of a finally block are executed when control leaves a try statement, whether the transfer of control occurs as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.


It is possible to use one try block and few catch blocks. If one type of exception require some action you can enter them into that block and leave finally block empty.


You can't - the finally block is always executed, unless for some reason the JVM exits before it gets to it. Link from MSDN

I'm not quite sure why you would want to do this, anyway. Sounds like you may need to change your design somewhat.


Alternative:

Use a boolean to determine which code in your finally block is called.

    public static int Main()
    {
        bool complete = false;
        try
        {
            Console.WriteLine("First try block");

            try
            {
                Console.WriteLine("Second try block");
                //exit(0);
                //Environment.Exit(0);
                return 0;
            }

            finally
            {
                Console.WriteLine("Finally block of inner try");
            }
        }

        finally
        {
            if (complete)
            {
                Console.WriteLine("Finally block of second try");
            }
        }
    }

Make sure to set your complete variable somewhere.


    static void Main(string[] args)
    {
        try
        {
            int a = 10/0;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {

            while (true)
            {
                Console.WriteLine("Statement 1");
                break;
                Console.WriteLine("Statement 2");
            }
        }
        Console.ReadLine();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消