.NET:异常处理的两条“黄金定律”,求批!
背景
架构之处必须考虑:如何处理异常?如何定义自己的异常体系?本文为了强化这个概念而写。
异常处理的两条“黄金定律”
自己抄袭的两条规律:
- 异常不能穿过“边界类”。
- 异常不能在没有恢复的情况下“吞掉”。
我们会将异常分为两类:“需要恢复”和“不需要恢复”,“需要恢复”的异常如果到达了边界类,就说明系统有BUG了,这类异常需要记录到日志。“不需要恢复”的异常需要进一步分为:“我们不能恢复”和“我们不期望恢复”,如果这类异常到达边界类,“我们不能恢复“的异常同样需要记录到日志,“我们不期望恢复”的异常则直接将异常信息显示给界面。一般采用AOP处理边界异常。
示例
AOP
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc; using Common.Logging;
using Happy.ExceptionHanding;
using Happy.Web.Mvc.Newtonsoft; namespace Happy.Web.Mvc.ExceptionHanding
{
/// <summary>
/// 处理应用程序未捕获的异常。
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class WriteExceptionResultAttribute : FilterAttribute, IExceptionFilter
{
/// <inheritdoc />
public void OnException(ExceptionContext filterContext)
{
var exception = filterContext.Exception;
if (!FriendlyExceptionRegistry.IsFriendly(exception.GetType()))
{
LogManager.GetCurrentClassLogger().Error(exception);
}
filterContext.Result = CreateErrorResult(exception);
filterContext.ExceptionHandled = true;
} private static ActionResult CreateErrorResult(Exception exception)
{
var information = ExceptionInformationProviderRegistry.CreateInformation(exception); return new NewtonsoftJsonResult
{
Data = information
};
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using Happy.ExtentionMethods; namespace Happy.ExceptionHanding
{
/// <summary>
/// 异常信息提供者注册处。
/// </summary>
public static class ExceptionInformationProviderRegistry
{
private static readonly Dictionary<Type, IExceptionInformationProvider> _providers
= new Dictionary<Type, IExceptionInformationProvider>(); /// <summary>
/// 注册提供者。
/// </summary>
public static void Register<TException>(IExceptionInformationProvider provider)
where TException : Exception
{
Register(typeof(TException), provider);
} /// <summary>
/// 注册提供者。
/// </summary>
public static void Register(Type exceptionType, IExceptionInformationProvider provider)
{
exceptionType.MustNotNull("exceptionType");
provider.MustNotNull("provider"); _providers[exceptionType] = provider;
} public static Dictionary<string, object> CreateInformation(Exception exception)
{
exception.MustNotNull("exception"); var exceptionType = exception.GetType(); var information = CreateDefaultInformation(exception); if (_providers.ContainsKey(exceptionType))
{
var extInformation = _providers[exceptionType].CreateInformation(exception); foreach (var item in extInformation.ToDictionary())
{
information[item.Key] = item.Value;
}
}
else
{
if (FriendlyExceptionRegistry.IsFriendly(exception.GetType()))
{
information["exception"] = Resource.Messages.Msg_DefaultExceptionMessage;
}
} return information;
} private static Dictionary<string, object> CreateDefaultInformation(Exception exception)
{
return new Dictionary<string, object>
{
{ "success", false },
{ "exception", exception.GetType().Name },
{ "message",exception.Message }
};
}
}
}
备注
放弃继续玩 GO 的一个原因就是:GO 的异常处理太不爽了,或者是我自己的原因,不够 OPEN。
.NET:异常处理的两条“黄金定律”,求批!的更多相关文章
- 《转》前端性能优化----yahoo前端性能团队总结的35条黄金定律
除了自己总结:1. 减少http请求,2.压缩并优化js/css/image 3.尽量静态页面,从简原则 4.代码规范(详见:个人知识体系思维导图) 从yahoo 新学到的: 网页内容 减少http请 ...
- 前端性能优化----yahoo前端性能团队总结的35条黄金定律
转自 http://www.cnblogs.com/lei2007/archive/2013/08/16/3262897.html
- c编程:求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。
//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max, ...
- C++ 根据两点式方法求直线并求两条直线的交点
Line.h #pragma once //Microsoft Visual Studio 2015 Enterprise //根据两点式方法求直线,并求两条直线的交点 #include"B ...
- 求空间内两条直线的最近距离以及最近点的坐标(C++)
关键词:空间几何 用途:总有地方会用到吧 文章类型:C++函数展示 @Author:VShawn(singlex@foxmail.com) @Date:2016-11-19 @Lab: CvLab20 ...
- Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)
http://poj.org/problem?id=1269 我今天才知道原来标准的浮点输出用%.2f 并不是%.2lf 所以wa了好几次 题目大意: 就给你两个线段 然后求这两个线段所在的 ...
- 全是干货!UI设计的30条黄金准则!
http://www.wex5.com/portfolio-items/js-1/ 全是干货!UI设计的30条黄金准则! 总的来说,好的UI界面有几个特征:简洁.便利.目标明确.人性化.字面上看这 ...
- 两条直线(蓝桥杯)二分枚举+RMQ
算法提高 两条直线 时间限制:1.0s 内存限制:256.0MB 问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
随机推荐
- 三十分钟理解博弈论“纳什均衡” -- Nash Equilibrium
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 纳什均衡(或者纳什平衡),Nash ...
- (转)Opencv卷积操作
转自:http://www.2cto.com/kf/201312/267308.html Mask Operation filter2D函数 Last Edit 2013/12/24 所谓的Mask ...
- Jmeter-----保存到响应文件
在jmeter中使用保存响应到文件 ------适用于非GUI模式执行脚本时,无法查看报错的信息. 1.添加组件: 2.各个配置项说明: 1.名称:即组件在整个测试计划中的名称显示,建议设置为用意义的 ...
- hdu4307
好题,详细题解在这里http://blog.csdn.net/weiguang_123/article/details/8077385 这里回顾一下: 当i和j都在一个集合里会产生新的收益,是经典题直 ...
- 【LOJ】 #2132. 「NOI2015」荷马史诗
题解 k叉哈夫曼树,但是没有了二叉那样的最后一定能合并成一个树根的优秀性质,我们就不断模拟操作看看到了哪一步能用的节点数< k,然后先拿这些节点数合并起来 然后就可以k个k个合并了,大小一样先拿 ...
- Adsafe 导致win10 中窗口错位
域账号使用,出现上述情况,干掉后一切恢复正常... 还好家里的本地管理员账号使用一切正常,不然又被广告占领了
- spring_150904_hibernatetemplate
实体类: package com.spring.model; import javax.persistence.Entity; import javax.persistence.Id; import ...
- JQuery中$.ajax()方法参数详解 ASP.NET jquery ajax传递参数
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- Qt基础——让使用Designer创建的UI也能自动适应窗口大小
原文请看:http://www.cnblogs.com/linmeng/archive/2012/07/05/2559259.html 我们知道,通过Qt的各种Layout可以实现控件的自动布局. 但 ...
- JQuery插件ajaxFileUpload 异步上传文件(PHP版)
太久没写博客了,真的是太忙了.善于总结,进步才会更快啊.不多说,直接进入主题. 前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错 ...