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

推荐订阅源

F
Fortinet All Blogs
罗磊的独立博客
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
博客园 - 聂微东
博客园_首页
爱范儿
爱范儿
量子位
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Martin Fowler
Martin Fowler
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Vercel News
Vercel News
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
Engineering at Meta
Engineering at Meta

博客园 - 赤脚小子

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的部分有些过时了,现在的异常体系并没有严格遵守这种分类规则。