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

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

时间: 2024-11-10 15:23:15

CoreCLR on Mac:体验managed exception handling的相关文章

[转载]A Crash Course on the Depths of Win32 Structured Exception Handling

转自:[已完工][经典文章翻译]A Crash Course on the Depths of Win32 Structured Exception Handling 原文题目: <<A Crash Course on the Depths of Win32™ Structured Exception Handling>> 原文地址: http://www.microsoft.com/msj/0197/Exception/Exception.aspx 原作者: Matt Pietr

OAF_OAF Exception Handling异常处理(概念)

2014-06-12 BaoXinjian 一.摘要 Oracle Application Framework将异常分为三类 异常类型type 1. 常规异常General Exception Class:oracle.apps.fnd.framework.OAException Function: 提供了在运行时刻同时显示多种类型异常的手段,结合EBS的Message,可显示有用的信息 2. 验证异常Validation Exception Class:oracle.apps.fnd.fram

/EH (Exception Handling Model)

Specifies the kind of exception handling used by the compiler, when to optimize away exception checks, and whether to destroy C++ objects that go out of scope because of an exception. If /EH is not specified, the compiler catches both asynchronous st

Exception handling

https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler. The details of how this is done depends on whether it is a hardware or software exception a

[转]java-Three Rules for Effective Exception Handling

主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in Java provide a consistent mechanism for identifying and responding to error conditions. Effective exception handling   will make your programs more ro

Declarative Exception Handling in Struts

Declarative Exception Handling is the procedure of handling Exceptions declaratively through the help of xml files. It is not needed to include exception-handling code in the java code of the application. The way of handling the exception would be sp

Exception Handling in ASP.NET Web API webapi异常处理

原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes error and exception handling in ASP.NET Web API. HttpResponseException Exception Filters Registering Exception Filters HttpError HttpResponseException Wha

SEH(structured exception handling)基础篇---终止处理程序

前序:最近看SEH看的头晕脑胀/(ㄒoㄒ)/~~,SEH最开始是Windows提供的异常处理机制,只是一个简单的框架,而我们现在使用的SEH都是编译器已经在系统提供的最基础的框架上做了修改的增强版(原始版比较原始,牵扯到大量Windows基础知识,并且需要反汇编看汇编代码来理解....本人现在功力较浅,需要慢慢消化,先看一下增强版,通过C和C++来接触一下. SEH包含两部分功能:终止处理(termination handling)和异常处理(exception handling).在这先讲一下

Exception Handling Statements (C# Reference)

Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous situations, known as exceptions, which may occur during the execution of your program. These exceptions are handled by code that is outside the normal fl