回到目录

对于业务层的程序的致命错误,我们一直的做法就是直接抛出指定的异常,让程序去终断,这种做法是对的,因为如果一个业务出现了致命的阻塞的问题,就没有必要再向上一层一层的返回了,但这时有个问题,直接抛异常,意味着服务器直接500了,前端如何去显示,或者如果你是API的服务,如果为前端返回,如果是500,那直接就挂了,哈哈!

下面是在MVC环境下优化的全局异常捕获代码(非API)

    /// <summary>
/// 全局异常捕获
/// </summary>
public class GlobalExceptionFilterAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
JsonResult response = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
if (context.Exception is ArgumentException)
{
var exception = (ArgumentException)context.Exception;
response.Data = new
{
statusCode = System.Net.HttpStatusCode.InternalServerError,
errorcode = "",
message = exception.Message,
};
}
else if (context.Exception is Exception)
{
var exception = (Exception)context.Exception;
response.Data = new
{
statusCode = System.Net.HttpStatusCode.InternalServerError,
errorcode = "",
message = exception.Message,
};
}
else
{ response.Data = new
{
statusCode = System.Net.HttpStatusCode.InternalServerError,
errorcode = "",
message = "其它异常",
};
} context.Result = response;
context.ExceptionHandled = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.TrySkipIisCustomErrors = true;
} }

如果业务层有问题,直接就throw了

   if (id == )
throw new ArgumentException("id不能为0");
if (id < )
throw new ArgumentException("id不能是负数");

然后页面后,故意让它抛出异常,我们看一下页面响应的结果

事实上,对于服务器来说,它是200,正常返回的,而对不业务模块来说,它的状态是个500,呵呵,这点要清楚.

感谢各位阅读!

回到目录

爱上MVC~业务层刻意抛出异常,全局异常的捕获它并按格式返回的更多相关文章

  1. SpringBoot 全局异常拦截捕获处理

    一.全局异常处理 //Result定义全局数据返回对象 package com.xiaobing.demo001.domain; public class Result { private Integ ...

  2. python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中

    使用sys.excepthook函数进行全局异常的获取. 1. 使用MessageDialog实现异常显示: 2. 使用logger把捕获的异常信息输出到日志中: 步骤:定义异常处理函数, 并使用该函 ...

  3. SpringMvc 全局异常处理器定义,友好的返回后端错误信息

    import com.google.common.collect.Maps; import org.apache.log4j.Logger; import org.springframework.be ...

  4. Spring MVC中@ControllerAdvice注解实现全局异常拦截

    在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...

  5. 【spring源码学习】spring配置的事务方式是REQUIRED,但业务层抛出TransactionRequiredException异常问题

    (1)spring抛出异常的点:org.springframework.orm.jpa.EntityManagerFactoryUtils public static DataAccessExcept ...

  6. SpringBoot全局异常的捕获设置

    1.新建立一个捕获异常的实体类 如:LeeExceptionHandler package com.leecx.exception; import javax.servlet.http.HttpSer ...

  7. C# WinForm捕获全局异常(捕获未处理的异常)

    static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...

  8. MVC 全局异常过滤器HandleErrorAttribute

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. springBoot 全局异常捕捉

    package cn.com.cs.core.exception; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import or ...

随机推荐

  1. Centos7 忘记密码的情况下,修改root或其他用户密码

    转载:https://blog.csdn.net/wcy00q/article/details/70570043 应用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于ce ...

  2. Struts2 配置项

    基础Constants struts.devMode  可选值true,false (默认false),在开发模式下,struts2的动态重新加载配置和资源文件的功能会默认生效.同时开发模式下也会提供 ...

  3. how to download a file with Nodejs(without using third-party libraries)用node下载文件

    创建HTTP GET请求并将其管理response到可写文件流中: var http = require('http'); var fs = require('fs'); var file = fs. ...

  4. 报错:Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): cn.itcast.bos.domain.base.SubArea

    因为 实体类中的主键 是String类型 不能自动为其分配id  所以需要手动设置在service层   model.setId(UUID.randomUUID().toString());

  5. 2、misa统计SRR结果

    参考: https://www.sogou.com/link?url=hedJjaC291NYNxVe4xgB4c3bUxXRMqZrT93cntTAgYfyBbRAdP9kIA.. https:// ...

  6. Lucene.net 搜索引擎的中文资料

    以下是我找到的网上一些关于Lucene.net 搜索引擎的介绍资料 https://code.i-harness.com/zh-CN/tagged/lucene?page=5 http://jingp ...

  7. superset 错误解决

    访问superset localhost:8088   securety->list Role 报错 xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx ...

  8. Libvirt代码架构

    Libvirt介绍 参考资料 Libvirt学习 通过virsh了解libvirt api的调用方式 通过virHypervisorDriver了解libvirt api的实现 virsh代码阅读 通 ...

  9. Codeforces#514D(三分,简单二维几何)

    #include<bits/stdc++.h>using namespace std;const double eps=1e-8;int n; struct node{    double ...

  10. Ubuntu常用命令集合

    文件操作 查看当前目录: pwd 参考文章:https://blog.csdn.net/qq_33421080/article/details/76551554 应用编辑类 安装: sudo apt- ...