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

相关推荐

  1. C#定义异常Exception实现

    2024-07-17 23:10:05       25 阅读
  2. C#面:什么是定义异常

    2024-07-17 23:10:05       36 阅读
  3. 实现C++定义String类

    2024-07-17 23:10:05       31 阅读
  4. SpringBoot实现定义异常+全局异常统一处理

    2024-07-17 23:10:05       53 阅读
  5. 异常Exception

    2024-07-17 23:10:05       32 阅读
  6. 认识异常定义异常

    2024-07-17 23:10:05       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 23:10:05       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 23:10:05       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 23:10:05       62 阅读
  4. Python语言-面向对象

    2024-07-17 23:10:05       72 阅读

热门阅读

  1. JDK 方法中的小坑

    2024-07-17 23:10:05       21 阅读
  2. LVS集群简介

    2024-07-17 23:10:05       22 阅读
  3. 类和对象-多态-纯虚函数和抽象类

    2024-07-17 23:10:05       21 阅读
  4. 建筑产业网元宇宙的探索与实践

    2024-07-17 23:10:05       21 阅读
  5. 微信小程序加载动画文件

    2024-07-17 23:10:05       29 阅读
  6. 刷题记录:LeetCode 925.长按键入

    2024-07-17 23:10:05       24 阅读
  7. 实际项目中JVM调优

    2024-07-17 23:10:05       20 阅读
  8. 如何做到思维的顺畅运行

    2024-07-17 23:10:05       20 阅读