zoukankan      html  css  js  c++  java
  • C#自定义异常 统一异常处理

    异常类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EasErpQuerySys.EasApplication
    {
        [Serializable]
        public class EasWebServiceException : ApplicationException
        {
            private readonly ExceptionResult _exceptionResult;
            public EasWebServiceException() { }
            public EasWebServiceException(string resultStatus, string resultMsg) : base(resultStatus)
            {
                _exceptionResult = new ExceptionResult { resultMsg = resultMsg, resultStatus = resultStatus };
            }
            public ExceptionResult GetExceptionResult()
            {
                return _exceptionResult;
            }
        }
        public class ExceptionResult
        {
            public string resultStatus { get; set; }
            public string resultMsg { get; set; }
        }
    }

    触发类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EasErpQuerySys.EasApplication
    {
        public class EasAppService : IEasAppService
        {
            public string Test()
            {
    
                throw new EasWebServiceException("失败", "失败了滚蛋");
    
            }
        }
    }

    捕获类

    using EasErpQuerySys.EasApplication;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                IEasAppService easAppService = new EasAppService();
    
    
                try
                {
                    easAppService.Test();
                }
                catch (EasWebServiceException e)
                {
                    var tt = e.GetExceptionResult();
                    Console.WriteLine(tt.resultMsg);
                    Console.WriteLine(tt.resultStatus);
                    Console.ReadLine();
                }
            }
        }
    }

    输出结果:

  • 相关阅读:
    【linux】驱动-5-驱动框架分层分离&实战
    【linux】驱动-4-LED芯片手册分析
    【MCU】国民N32固件库移植
    【MCU】移植AT32库&FreeRTOS教程
    P3768 简单的数学题
    P4301 [CQOI2013] 新Nim游戏
    P4767 [IOI2000]邮局
    P3211 [HNOI2011]XOR和路径
    FWT 笔记
    P3175 [HAOI2015]按位或(max-min 容斥)
  • 原文地址:https://www.cnblogs.com/eedc/p/9266237.html
Copyright © 2011-2022 走看看