zoukankan      html  css  js  c++  java
  • 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!
    " + 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.
    " + e.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Test D: success! Caught exception!
    " + 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!
    " + ex.ToString());
            }
            finally
            {
                Console.WriteLine("Main.finally().");
            }
    
            Console.WriteLine("
    Testing B");
    
            B();
    
            Console.WriteLine("
    Testing D");
    
            D();
    
            Console.WriteLine("
    Testing 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.
    " + 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!
    " + 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

  • 相关阅读:
    移动端-纯css隐藏滚动条解决方案
    阻止点击穿透
    JS的防抖与节流
    go 自动安装项目依赖包
    git 修改远程仓库
    git 基础命令
    go 包govalidator
    go email
    windows下Redis的安装和使用
    go xorm,负载均衡
  • 原文地址:https://www.cnblogs.com/dudu/p/coreclr-mac-managed-exception-handling.html
Copyright © 2011-2022 走看看