C#自定义异常(Exception)的实现

1、自定义异常类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionApp
{
    public class CustomException:Exception
    {
        //默认构造函数
        public CustomException():base() { }
        //接收错误信息的构造函数
        public CustomException(string message) : base(message) { }
        //同时接收错误信息和内部异常的构造函数
        public CustomException(string message,Exception inner) : base(message,inner) { }

        //添加额外的属性或方法以适应特定的需求
        public string AdditionalInfo { get; set; }
        //按需要传递信息
        public CustomException(string message,string additionalInfo) : base(message) { 
            AdditionalInfo = additionalInfo;
        }
    }
}

2、默认自定义异常

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //默认自定义异常
                throw new CustomException();
            }
            catch (CustomException ex)
            {
                Console.WriteLine($"Message:{ex.Message},AdditionalInfo:{ex.AdditionalInfo}");
            }
            finally { 
                Console.ReadLine(); 
            }
        }
    }
}

3、接收错误信息的自定义异常

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //接收错误信息的自定义异常
                throw new CustomException("自定义异常信息");
            }
            catch (CustomException ex)
            {
                Console.WriteLine($"Message:{ex.Message},AdditionalInfo:{ex.AdditionalInfo}");
            }
            finally { 
                Console.ReadLine(); 
            }
        }
    }
}

4、同时接收错误信息和内部异常的自定义异常

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //默认自定义异常
                //同时接收错误信息和内部异常的自定义异常
                throw new CustomException("自定义异常", new DivideByZeroException());
            }
            catch (CustomException ex)
            {
                Console.WriteLine($"Message:{ex.Message},AdditionalInfo:{ex.AdditionalInfo}");
            }
            finally { 
                Console.ReadLine(); 
            }
        }
    }
}

5、按需要传递信息自定义异常

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //按需要传递信息自定义异常
                throw new CustomException("自定义异常信息", "505");
            }
            catch (CustomException ex)
            {
                Console.WriteLine($"Message:{ex.Message},AdditionalInfo:{ex.AdditionalInfo}");
            }
            finally { 
                Console.ReadLine(); 
            }
        }
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中,我们可以通过自定义异常类来处理应用程序中可能出现的异常情况。自定义异常类需要继承自`System.Exception`类。 以下是一个简单的自定义异常类的实现示例: ```csharp public class MyException : Exception { public MyException() { } public MyException(string message) : base(message) { } public MyException(string message, Exception innerException) : base(message, innerException) { } } ``` 在上面的代码中,我们定义了一个名为`MyException`的自定义异常类。该类继承自`System.Exception`类,并提供了三个构造函数。第一个构造函数是默认的构造函数,第二个构造函数接受一个字符串参数,用于指定异常消息,第三个构造函数接受两个参数,第一个参数是异常消息,第二个参数是内部异常。 通过自定义异常类,我们可以在应用程序中抛出符合我们需求的异常,这样可以更好地处理应用程序中可能出现的异常情况。以下是一个示例: ```csharp public class MyClass { public void DoSomething(int input) { if (input < 0) { throw new MyException("Input cannot be negative."); } } } class Program { static void Main(string[] args) { MyClass myClass = new MyClass(); try { myClass.DoSomething(-1); } catch (MyException ex) { Console.WriteLine(ex.Message); } } } ``` 在上面的代码中,我们在`MyClass`类的`DoSomething`方法中检查输入是否为负数,如果是负数,则抛出`MyException`异常。在`Main`方法中,我们捕获了`MyException`异常,并输出了异常消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值