惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
U
Unit 42
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
Recent Announcements
Recent Announcements
B
Blog RSS Feed
罗磊的独立博客
博客园 - Franky
J
Java Code Geeks
S
SegmentFault 最新的问题
D
DataBreaches.Net
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
腾讯CDC
博客园_首页
美团技术团队
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏

博客园 - 赤脚小子

Workflow WF Reference Links for 2009-03-20 Workflow WF Reference Links for 2009-03-06 Workflow WF Reference Links for 2009-02-20 Workflow WF Reference Links for 2009-02-13 Workflow WF Reference Links for 2009-02-06 Workflow WF Reference Links for 2009-02-01 Workflow WF Reference Links for 2009-01-16 Workflow WF Reference Links for 2009-01-09 Workflow WF Reference Links for 2008-12-30 WF书籍导读(完整版) WF书籍推荐 Unity(七):使用场景Ⅲ:用于依赖注入(下) Unity系列目录 Unity(六):使用场景Ⅲ:用于依赖注入(上) Unity(五):使用场景Ⅱ:用于单例模式 Unity(四):使用场景Ⅰ:建立类型映射 Unity(三):快速入门 Unity(二):Unity是什么? Unity(一):从ObjectBuilder说起
Exception Management in .NET阅读笔记
赤脚小子 · 2008-02-15 · via 博客园 - 赤脚小子

前两天看到Haissam推荐的一片文章——Exception Management in .NET.

确实是篇好文章,虽然有些年头了,但是仍颇有价值。仔细一看,原来是P&P出的一个Guide。

以下是一些我觉得有用的要点:

异常处理流程

  • exception handling process.

Figure 2. Exception handling process

  • Processing of unhandled exceptions, see Figure 3.

Figure 3. Processing of unhandled exceptions

异常检测

You should only catch exceptions when you need to specifically perform any of the following actions:

  • Gather information for logging

  • Add any relevant information to the exception

  • Execute cleanup code

  • Attempt to recover

异常传播
There are three main ways to propagate exceptions:

  • Let the exception propagate automatically:With this approach, you do nothing and deliberately ignore the exception.

  • Catch and rethrow the exception:With this approach, you catch and react to the exception, and clean up or perform any other required processing within the scope of the current method. If you cannot recover from the exception, you rethrow the same exception to your caller.

catch(TypeAException e)

{

// Code to do any processing needed.

// Rethrow the exception

throw;

}

  • Catch, wrap, and throw the wrapped exception:If you cannot recover, wrap the exception in a new exception, and throw the new exception back to the caller. The InnerException property of the Exception class explicitly allows you to preserve a previously caught exception. This allows the original exception to be wrapped as an inner exception inside a new and more relevant outer exception.

catch(TypeBException e)

{

// Code to do any processing needed.

// Wrap the current exception in a more relevant

// outer exception and rethrow the new exception.

throw(new TypeCException(strMessage, e));

}

When to use inner exception
You should wrap an exception only when wrapping an exception can provide a more relevant exception to the caller.

管理未处理异常

When an unhandled exception propagates to the last point or boundary at which your application can handle the exception before returning to the user, your application can no longer recover from the exception. At this point, it must gather information, log information, send any notifications, perform any cleanup, and do any other processing needed before managing the communication of the exception condition

to the user.

收集信息

What is the required information, just see table 2:


ps:文中关于Exception Hierarchies的部分有些过时了,现在的异常体系并没有严格遵守这种分类规则。