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();
}
}
}
}
输出结果:

C#自定义异常 统一异常处理的更多相关文章
- js构建ui的统一异常处理方案(四)
上一篇我们介绍了统一异常处理方案的设计方案,这一篇我们将直接做一个小例子,验证我们的设计方案. 例子是一个todo的列表界面(页面代码参考于https://github.com/zongxiao/Dj ...
- 使用Spring MVC统一异常处理实战
1 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...
- spring boot / cloud (二) 规范响应格式以及统一异常处理
spring boot / cloud (二) 规范响应格式以及统一异常处理 前言 为什么规范响应格式? 我认为,采用预先约定好的数据格式,将返回数据(无论是正常的还是异常的)规范起来,有助于提高团队 ...
- Spring Boot中Web应用的统一异常处理
我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况.Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来 ...
- spring boot配置统一异常处理
基于@ControllerAdvice的统一异常处理 >.这里ServerException是我自定义的异常,和普通Exception分开处理 >.这里的RequestResult是我自定 ...
- 【SpringMVC学习07】SpringMVC中的统一异常处理
我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生.在开发中,不管是dao层 ...
- SpringBoot 统一异常处理
统一异常处理: @ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactor ...
- 基于spring boot的统一异常处理
一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...
- 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...
随机推荐
- codeforces 1041A Heist
electronic a.电子的 heist v.抢劫 in ascending order 升序 indice n.标记 device n.装置设备 staff n.职员 in arbitrary ...
- Kubelet bootstrap 流程
首先,什么是kubelet bootstrap?在安装 k8s worker node 时,基本上 worker 的初始状态仅仅是安装了 docker 和 kubelet,worker 需要一种机制跟 ...
- 连接mysql报错Access denied for user 'root'@'localhost' (using password: YES)解决办法
1.打开MySQL目录下的my.ini文件,在文件的最后添加一行“skip-grant-tables”(免密码登录),保存并关闭文件,重启MySQL服务. 2.通过命令行进入MySQL的BIN目录,输 ...
- JAVA类与类之间的全部关系简述+代码详解
本文转自: https://blog.csdn.net/wq6ylg08/article/details/81092056类和类之间关系包括了 is a,has a, use a三种关系(1)is a ...
- springboot整合zookeeper
在springboot中所有的整合都是以bean的形式注入对象,从数据库coon.redis conn.再到整合的zookeeper,依然是依照bean注入连接对象,通过zookeeper api对z ...
- 织梦会员 Warning: preg_replace(): The /e modifier is no longer supported, us...
http://php.net/manual/zh/reference.pcre.pattern.modifiers.php#reference.pcre.pattern.modifiers.eval ...
- c++ 面试题(操作系统篇)
1,消息队列: https://kb.cnblogs.com/page/537914/ 2,fork中父进程和子进程的资源联系: https://blog.csdn.net/weixin_422506 ...
- js+正则+单双引号问题
在处理用js动态添加表格时,表格中有正则表达式,其单双引号问题导致的添加表格不成功,和正则失效问题 <script type="text/javascript"> va ...
- 综合评价模型C++实现
1 综合评价模型建立步骤 综合评价模式是一种对一个或多个系统进行评价的模型.一般分为如下几个步骤: 选取评价指标,指标的选取应该具有独立性和全面性. 得到m×n测量矩阵,每一行表示一个带评价系统(共m ...
- 三 分析easyswoole源码(启动服务&TableManager,略提及Cache工具的原理)
前文连接,讲了es是如何启动swoole服务的. 里面有一个工具类TableManager.这个类为了处理进程间数据共享.是对swoole_table的一层封装swoole_table一个基于共享内存 ...