C# Time of finally execution

后端 未结 9 1606
情话喂你
情话喂你 2021-02-04 01:37

Take this code:

using System;

namespace OddThrow
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
              


        
9条回答
  •  醉话见心
    2021-02-04 02:02

    To add more into the mix, consider this:

    using System;
    namespace OddThrow
    {
        class Program
        {
            static void Main()
            {
                AppDomain.CurrentDomain.UnhandledException +=
                    delegate(object sender, UnhandledExceptionEventArgs e)
                {
                    Console.Out.WriteLine("In AppDomain.UnhandledException");
                };
                try
                {
                    throw new Exception("Exception!");
                }
                catch
                {
                    Console.Error.WriteLine("In catch");
                    throw;
                }
                finally
                {
                    Console.Error.WriteLine("In finally");
                }
            }
        }
    }
    

    Which on my system (Norwegian) shows this:

    [C:\..] ConsoleApplication5.exe
    In catch
    In AppDomain.UnhandledException
    
    Ubehandlet unntak: System.Exception: Exception!
       ved OddThrow.Program.Main() i ..\Program.cs:linje 24
    In finally
    

提交回复
热议问题