The finally block does not always execute in try finally

A finally block does not always xecute. The code in the try block could go into an infinite loop, the exception could rigger a “fail fast” (which takes the process down without running any finally locks), or someone could pull the power cord out of the wall.

eg.

static void TryFinallyDemo()
        {
            try
            {
                while(true)
                {
                    Console.WriteLine(DateTime.Now.ToString("yyyyMMddHHmmssffff"));
                }
            }
            finally
            {
                Console.WriteLine("Finally block does not always execute!");
            }
        }

 

posted @ 2020-02-28 15:48  FredGrit  阅读(171)  评论(0编辑  收藏  举报