理解Exception

exception理论版
1. handle what the function can handle (mean this function know how to handle this event).  for perf improvement, sometimes we just try/catch important process and input checking. in one word, handle what we can handle.
2. the latest layer(application layer) should handle all exceptions.  For web site, web architecture has predefine a Application_Error to handle all unhandled exception as latest defence.
3. every exception should be only processed once.  the throwed exception after process is new exception. e.g. exception throwed by logic layer is very technical, but we can handle and log this technical exception info and return the user layer a simple info.
4. exception include stack info and error info.
 
exception实用版
1. 在最后一层的重要逻辑或容易出错的地方catch逻辑exception,建议log.  这就意味着有些不重要的代码就不用catch了。
2. 其它层要处理它必须处理的exception,但实际上人不可能catch所有的(no one is a fool),所以只能先catch重要逻辑或容易出错的地方。
3. 在出错后加catch,demand on request.呵呵。 这样可以减少exception处理代码和人力。
4. 对于web层,一般我们无需加任何exception,因为微软已经为我们做好了错误的出口(asax)。
 
以下是我旧的理解。exception的处理是没有问题,但log就不好了。其实每一步的信息可以由堆栈来提供,而不是每个地方因为要log而去处理exception。
 
exception必须在正确的地方处理。log却是在exception生成到结束间的每一步都要记录的,这样可以跟踪exception。
 
Top layer
exception : 处理一切exception
log : log一切exception
 
Lower layer
1 可以屏蔽的exception
  exception : 处理exception并继续运行。
  log : log exception
.2 不可屏蔽的exception
  exception : 处理exception并停止运行,throw exception到上层。
  log : log exception
PS.
可以屏蔽的exception : 不影响运行。如数据库的连接,一次连不上,可以再来一次。但超过了最大连接此书就是不可屏蔽的exception了。
 
Service layer
simplify service. only logic, no exception, no log.
 
 
问题
1 怎么判断exception是否可以处理?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值