在C#中捕获自定义异常

在C#中捕获自定义异常

问题描述:

我已经创建了如下的自定义异常类

I have created a custom exception class as below

namespace testingEXception
{
    public class CustomException : Exception
    {            
        public CustomException()
        {
        }
        public CustomException(string message)
            : base(message)
        {
        }

        public CustomException(string message, Exception innerException)
            : base(message, innerException)
        {

        }
    }
}

我正在像这样的相同解决方案中从另一个项目抛出异常

I am throwing an exception from a different project in the same solution like this

namespace ConsoleApplication1
{
    public class testClass
    {
        public void compare()
        {
            if (1 > 0)
            {
                throw new CustomException("Invalid Code");
            }
        }
    }
}

并像这样捕捉它

    namespace testingEXception
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                testClass obj = new testClass();
                obj.compare();

            }
            catch (testingEXception.CustomException ex)
            {
                //throw;
            }
            catch (Exception ex)
            {
               // throw new CustomException(ex.Message);
            }

            Console.ReadKey();
        }
    }
}

问题是,尽管异常类型显示CustomException,但异常不会被第一个捕获捕获,而是被第二个捕获捕获.

The problem is, the exception is not getting caught by the first catch, but instead getting caught by the second catch, over though the type of exception shows CustomException.

您需要提供更多详细信息,以下代码输出"CustomException":

You need to provide more detail, the following code outputs "CustomException":

try
{
    throw new CustomException("Invalid code.");
}
catch (CustomException ex)
{
    System.Diagnostics.Trace.WriteLine("CustomException");
    throw;
}
catch (Exception ex)
{
    throw;
}

具有以下课程:

public class CustomException : Exception
{
    public CustomException()
    {
    }
    public CustomException(string message)
        : base(message)
    {
    }

    public CustomException(string message, Exception innerException)
        : base(message, innerException)
    {

    }
}

更新:

关于优化和优化throw:这不会发生,因为任何特定的代码块都不知道堆栈中较高位置的调用者是否可以捕获CustomException的代码.抛出异常是可见的副作用,并且CLI中有各种保证以确保这些可见的副作用保持可见.

Update:

With regard to optimizations and optimizing away a throw: this cannot happen because any particular block of code cannot know whether a caller higher up in the stack could have code to catch CustomException. Throwing an exception is a visible side-effect and there are various guarantees in the CLI to ensure those visible side-effects remain visible.

此外,尝试,捕获和最终阻止是CLI所说的受保护区域".这些区域的特殊之处在于,具有可见"副作用的区域内的操作无法对其可见的副作用进行重新排序.有关更多详细信息,请参见 http://lynk.at/qH8SHk

In addition, try, catch and finally blocks are "protected regions" in CLI-speak. These regions are special in that the operations within the region with "visible" side-effects cannot have their visible side-effects re-ordered. For some more detail, see http://lynk.at/qH8SHk and http://lynk.at/pJcg98