CoreCLR on Mac:体验managed exception handling

C#测试代码:

using System;

class Program
{
    static void A()
    {
        try
        {
            Console.WriteLine("Throwing an exception");
            throw new Exception("Did you catch it?");
        }
        finally
        {
            Console.WriteLine("A.finally()");
        }
    }

    static void B()
    {
        try
        {
            C();
            Console.WriteLine("Failure! Exception has not been thrown.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Test B: success! Caught exception!\n" + ex.ToString());
        }
        finally
        {
            Console.WriteLine("B.finally()");
        }
    }

    static void C()
    {
        A();
    }

    static void D()
    {
        try
        {
            try
            {
                Console.WriteLine("Throwing an exception");
                throw new Exception("Did you catch it in the right handler?");
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("Test D: Failed. Called wrong handler.\n" + e.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Test D: success! Caught exception!\n" + ex.ToString());
        }
    }

    static void Main()
    {
        Console.WriteLine("Testing A");
        try
        {
            A();
            Console.WriteLine("Failure! Exception has not been thrown.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Test A: success! Caught exception!\n" + ex.ToString());
        }
        finally
        {
            Console.WriteLine("Main.finally().");
        }

        Console.WriteLine("\nTesting B");

        B();

        Console.WriteLine("\nTesting D");

        D();

        Console.WriteLine("\nTesting class TestE");

        TestE.Run();

        Console.WriteLine("Exiting test");
    }

    class TestE
    {
        static void A()
        {
            try
            {
                Console.WriteLine("In A");
                B();
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine("Error! Caught ArgumentException.\n" + ae.ToString());
            }
        }

        static void B()
        {
            try
            {
                Console.WriteLine("In B");
                C();
            }
            finally
            {
                Console.WriteLine("Leaving B");
            }
        }

        static void C()
        {
            Console.WriteLine("In C");
            D();
            Console.WriteLine("Error! A() should not be reached in C()");
            A();
        }

        static void D()
        {
            Console.WriteLine("In D, throwing...");
            throw new Exception("Exception test");
        }

        public static void Run()
        {
            try
            {
                A();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Test C: success! Caught exception!\n" + ex.ToString());
            }
        }
    }
}
Throw Exception

代码编译:

mcs -nostdlib  -r:compile_r_lib/mscorlib.dll -r:compile_r_lib/System.Runtime.dll -r:compile_r_lib/System.Console.dll app/ThrowException.cs

代码运行:

runtime_mac/corerun app/ThrowException.exe

在没有实现managed exception handling时的运行结果:

Testing A
Throwing an exception
{0x7fff7c0e1300-0x108e7c840} ASSERT [DEBUG  ] at 
/git/dotnet/coreclr/src/pal/src/arch/i386/context.cpp.38: 
Trace/BPT trap: 5

在初步实现managed exception handling后的运行结果:

Testing A
Throwing an exception
A.finally()
Test A: success! Caught exception!
System.Exception: Did you catch it?
   at Program.A()
   at Program.Main()
Main.finally().

Testing B
Throwing an exception
A.finally()
Test B: success! Caught exception!
System.Exception: Did you catch it?
   at Program.A()
   at Program.B()
B.finally()

Testing D
Throwing an exception
Test D: success! Caught exception!
System.Exception: Did you catch it in the right handler?
   at Program.D()
Trace/BPT trap: 5

对应的git提交:Implement basic support for managed exception handling

对应的github pull request:Implement basic support for managed exception handling

posted @ 2015-03-04 12:05 dudu 阅读(...) 评论(...) 编辑 收藏