小编Hat*_*oin的帖子

在C#中捕获自定义异常

我创建了一个自定义异常类,如下所示

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

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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

并像这样抓住它

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

            }
            catch …
Run Code Online (Sandbox Code Playgroud)

c# custom-exceptions

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

custom-exceptions ×1