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

推荐订阅源

F
Fortinet All Blogs
WordPress大学
WordPress大学
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
D
Docker
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
U
Unit 42
M
MIT News - Artificial intelligence
B
Blog
GbyAI
GbyAI
C
Check Point Blog
P
Proofpoint News Feed
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
IT之家
IT之家
Google DeepMind News
Google DeepMind News
V
V2EX
Stack Overflow Blog
Stack Overflow Blog

博客园 - 赤脚小子

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