.NET Core: Finally block not called on unhandled exception on Linux

前端 未结 2 1325
不知归路
不知归路 2021-01-08 00:20

I have created the following C# program:

namespace dispose_test
{
    class Program
    {
        static void Main(string[] args)
        {
            using         


        
2条回答
  •  别那么骄傲
    2021-01-08 00:46

    If you surround this with a try-catch, the finally block will run when the exception is caught (handled) by the outer catch.

        static void Main(string[] args)
        {
            try
            {
                using (var disp = new MyDisposable())
                {
                    throw new Exception("Boom");
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
    

提交回复
热议问题