Bootstrap

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(); 
            }
        }
    }
}
;