码迷,mamicode.com
首页 > 系统相关 > 详细

CoreCLR on Mac:体验managed exception handling

时间:2015-03-04 12:36:46      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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

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

CoreCLR on Mac:体验managed exception handling

标签:

原文地址:http://www.cnblogs.com/dudu/p/coreclr-mac-managed-exception-handling.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!