SuiFei
人的一生,或多或少,总是难免有浮沉。不会永远如旭日东升,也不会永远痛苦潦倒。只有面对现实,才能超越现实
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 138 文章- 3 评论- 1223
2010年1月12日
异常处理准则
Never do a "catch" exception and do nothing. If you hide an exception, you will never know if the exception happened or not.
In case of exceptions, give a friendly message to the user, but log the actual error with all possible details about the error, including the time it occurred, method and class name etc.
Always catch only the specific exception, not generic exception as well as system exceptions.
You can have an application level (thread level) error handler where you can handle all general exceptions. In case of an 'unexpected general error', this error handler should catch the exception and should log the error in addition to giving a friendly message to the user before closing the application, or allowing the user to 'ignore and proceed'.
Do not write try-catch in all your methods. Use it only if there is a possibility that a specific exception may occur. For example, if you are writing into a file, handle only FileIOException.
Do not write very large try-catch blocks. If required, write separate try-catch for each task you perform and enclose only the specific piece of code inside the try-catch. This will help you find which piece of code generated the exception and you can give specific error message to the user.
You may write your own custom exception classes, if required in your application. Do not derive your custom exceptions from the base class SystemException. Instead, inherit from ApplicationException.
To guarantee resources are cleaned up when an exception occurs, use a try/finally block. Close the resources in the finally clause. Using a try/finally block ensures that resources are disposed even if an exception occurs.
Error messages should help the user to solve the problem. Never give error messages like "Error in Application", "There is an error" etc. Instead give specific messages like "Failed to update database. Make sure the login id and password are correct."
When displaying error messages, in addition to telling what is wrong, the message should also tell what the user should do to solve the problem. Instead of message like "Failed to update database.” suggest what should the user do: "Failed to update database. Make sure the login id and password are correct."
Show short and friendly message to the user. But log the actual error with all possible information. This will help a lot in diagnosing problems.
Define a global error handler in Global.asax to catch any exceptions that are not handled in code. You should log all exceptions in the event log to record them for tracking and later analysis.
posted @
2010-01-12 11:13
萧寒 阅读(934) |
评论(3)
|
编辑
公告
多少事,从来急;
天地转,光阴迫。
一万年太久,只争朝夕。
我的主页
个人资料
我的闪存
发短消息
<
2010年1月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
最新闪存
准备回家了!
09-27 11:50
更多闪存...
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
最新评论
我的标签
我参与的团队
湖南.NET俱乐部(0/0)
O/R Mapping团队(0/0)
VSTS团队(0/0)
Silverlight学习与研究(0/0)
Web页快照项目(0/0)
Debug 探索团队(0/0)
我的标签
vsts(2)
vstscodehelper(2)
source code(1)
Unit(1)
单元测试(1)
vsunit(1)
infragistics(1)
ultrawebGrid(1)
固定列(1)
行高(1)
随笔分类
(211)
.NET WebForms(18)
(rss)
C# WinForms(73)
(rss)
Delphi(9)
(rss)
Javascript(1)
(rss)
VS Unit(1)
(rss)
Windows 7(1)
(rss)
连连看.NET(8)
(rss)
模拟泡泡堂(2)
(rss)
日记(31)
(rss)
视频
(rss)
收藏(9)
(rss)
外挂(7)
(rss)
小知识(34)
(rss)
游戏新闻(17)
(rss)
随笔档案
(133)
2010年2月 (2)
2010年1月 (12)
2009年12月 (1)
2009年10月 (1)
2009年9月 (1)
2009年8月 (1)
2008年12月 (1)
2008年11月 (2)
2008年9月 (1)
2008年7月 (2)
2008年6月 (2)
2008年4月 (1)
2008年3月 (1)
2008年2月 (1)
2007年11月 (1)
2007年4月 (1)
2007年3月 (1)
2006年12月 (7)
2006年9月 (4)
2006年8月 (3)
2006年7月 (5)
2006年6月 (4)
2006年4月 (3)
2006年3月 (1)
2006年2月 (1)
2006年1月 (1)
2005年12月 (6)
2005年10月 (4)
2005年9月 (1)
2005年8月 (4)
2005年7月 (6)
2005年6月 (11)
2005年5月 (18)
2005年4月 (22)
文章分类
(3)
C# 2.0(1)
(rss)
C#与游戏编程(2)
(rss)
IBM Websphere MQ
(rss)
文章档案
(3)
2006年6月 (1)
2005年5月 (2)
相册
小片段
博客园
alittlefish
(rss)
鱼遇于池,池涸,相濡以沫,相鞠以湿,不若相忘于海。
FireScript地带
(rss)
FireReprt◇FireScript地带
个人收藏
MSDN(中文)
MSDN
my msn blog.
我的msn Blog.
notreg
notreg
WorldLingo
WorldLingo: 翻译、本地化、全球化
电驴下载
分享互联网
构件EOS
我在腾讯的个人文集
我在腾讯的个人文集,那时候混qq论坛,但最后腾讯取消asp版面,就结束了.
字幕搜索
射手网 - Shooter.com.cn - 与别人分享,别人与你分享
朋友
音乐虫子
(rss)
音乐虫子
相关工具
Convert C# to vbnet
This utility will allow you to convert your C# code to its equivalent in VB.NET.
Convert vbnet to C#
This utility will allow you to convert your VB.NET code to its equivalent in C#.
pinvoke
.net 的api库
看地图
卫星地图
友人[手机网]
全都是手机,还有介绍
最新随笔
1. 生成GBK的文本
2. Syncfusion Essential DocIO操作word文件实用函数
3. oracle笔记(2010-1-30)
4. 动态调用对象事件
5. DisC-Decompiler for TurboC
6. 眼泪原来可以这样笑出来
7. 有点精辟的东西
8. 一句代码提升进程权限
9. 异常处理准则
10. JetBrains ReSharper 5.x 注册机
11. 我的开发环境配置经验
12. Windows 特殊文件夹小全
积分与排名
积分 - 381931
排名 - 127
最新评论
1. Re:Resharper 5.x keygen 银光版(Silverlight测试下)
博主上还是撤下来吧,这样做不太好。 (王者归来)
2. Re:JetBrains ReSharper 5.x 注册机
非常感谢,刚好试用到期了 (坏人Sir)
3. Re:C#单元测试
也给我来一份吧,zhiyuanfirst@qq.com 谢谢啦! (yearN)
4. Re:动态调用对象事件
非常好用,谢谢!!! (niky)
5. Re:Syncfusion Essential DocIO操作word文件实用函数
支持在Silverlight的客户端使用吗? (WASPCN)
阅读排行榜
1. 传奇龙版地图查看器(16002)
2. WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法(14569)
3. 如何删除微软拼音输入法2003(12161)
4. 通过IViewObject接口,取浏览器的图象,实现SNAP(9175)
5. Pivot Table 实现详解(一)(8695)
评论排行榜
1. C#休闲作品【连连看】开发心得与作品下载(62)
2. 将执行文件转化为bat批处理文件的工具(批处理文件方式提供)(56)
3. 连连看.NET 1.41全部源码(42)
4. 通过IViewObject接口,取浏览器的图象,实现SNAP(41)
5. 调用worldlingo.com翻译服务,制作的一个多国语言翻译工具(33)
60天内阅读排行
1. 动态调用对象事件(1281)
2. Syncfusion Essential DocIO操作word文件实用函数(1001)
3. DisC-Decompiler for TurboC(58)
4. 生成GBK的文本(42)
5. oracle笔记(2010-1-30)(35)