Effective C# 学习笔记(四十六)对异常进行分类并逐类处理
2011-08-06 22:05
375 查看
对于异常的认识
异常并不是包括所有错误条件
抛出的异常最好是有定义的针对特殊类别的异常类型,不要总是使用System.Exception来处理异常,这样你可以针对不同的异常进行不同的Catch操作。可以从以下方面定义(这里只是抛砖引玉):
找不到文件或目录
执行权限不足
丢失网络资源
创建异常的要点
自定义异常类必须以Exception结尾
自定义异常类总是继承自System.Exception类
实现以下四个构造器重载
// Default constructor
public Exception();
// Create with a message.
public Exception(string);
// Create with a message and an inner exception.
public Exception(string, Exception);
// Create from an input stream. 支持序列化的
protected Exception(SerializationInfo, StreamingContext);
举例:
[Serializable]
public class MyAssemblyException :Exception
{
public MyAssemblyException() : base()
{
}
public MyAssemblyException(string s) : base(s)
{
}
public MyAssemblyException(string s, Exception e) : base(s, e)
{
}
//注意这里的可序列化的构造函数用了protected关键字,限制了访问权限
protected MyAssemblyException( SerializationInfo info, StreamingContext cxt) : base(info, cxt)
{
}
}
这里特别说下,带有内部异常参数的构造器重载,对于调用地方类库的方法的时候,要捕获地方的异常,并把其包装到自己的异常中进行封装抛出:
public double DoSomeWork()
{
try {
// This might throw an exception defined
// in the third party library:
return ThirdPartyLibrary.ImportantRoutine();
} catch(ThirdPartyException e)
{
string msg =
string.Format("Problem with {0} using library",ToString());
throw new DoingSomeWorkException(msg, e);
}
}
异常并不是包括所有错误条件
抛出的异常最好是有定义的针对特殊类别的异常类型,不要总是使用System.Exception来处理异常,这样你可以针对不同的异常进行不同的Catch操作。可以从以下方面定义(这里只是抛砖引玉):
找不到文件或目录
执行权限不足
丢失网络资源
创建异常的要点
自定义异常类必须以Exception结尾
自定义异常类总是继承自System.Exception类
实现以下四个构造器重载
// Default constructor
public Exception();
// Create with a message.
public Exception(string);
// Create with a message and an inner exception.
public Exception(string, Exception);
// Create from an input stream. 支持序列化的
protected Exception(SerializationInfo, StreamingContext);
举例:
[Serializable]
public class MyAssemblyException :Exception
{
public MyAssemblyException() : base()
{
}
public MyAssemblyException(string s) : base(s)
{
}
public MyAssemblyException(string s, Exception e) : base(s, e)
{
}
//注意这里的可序列化的构造函数用了protected关键字,限制了访问权限
protected MyAssemblyException( SerializationInfo info, StreamingContext cxt) : base(info, cxt)
{
}
}
这里特别说下,带有内部异常参数的构造器重载,对于调用地方类库的方法的时候,要捕获地方的异常,并把其包装到自己的异常中进行封装抛出:
public double DoSomeWork()
{
try {
// This might throw an exception defined
// in the third party library:
return ThirdPartyLibrary.ImportantRoutine();
} catch(ThirdPartyException e)
{
string msg =
string.Format("Problem with {0} using library",ToString());
throw new DoingSomeWorkException(msg, e);
}
}
相关文章推荐
- Effective C# 学习笔记(四十七)对异常进行strong guarantee 策略处理
- Effective C# 学习笔记(三十七) 警惕并行处理中的异常处理
- 【Spring学习笔记-MVC-15】Spring MVC之异常处理
- swift语言的学习笔记四-2(对上一节有些遗留进行处理)
- Python学习笔记--异常处理
- Java Web 学习笔记之十一:RestEasy统一处理异常
- Effective C# 学习笔记(三十九) 使用Dynamic处理范型参数的运行时类型
- 【VS2010学习笔记】【异常处理】general error c1010070: Failed to load and parse the manifest.
- 业调度框架 Quartz 学习笔记(六) -- job生病了(抛出异常)时的处理
- 转载:学习笔记:oracle异常处理
- OpenCV学习笔记(8)VS2008 MFC下使用OpenCV2.0进行简单图像处理
- 微软企业库5.0学习笔记(四十二)异常处理模块
- Struts2学习笔记三(异常处理、模型驱动和属性驱动、servlet接口、Preparable接口)
- JavaSE学习笔记之-----异常处理
- java 异常处理 黑马程序员学习笔记(6)
- python 系统学习笔记(九)---异常处理
- Effective C# 学习笔记(四十三)使用Expression处理绑定(属性值更改)事件
- Guava学习笔记:简化异常处理的Throwables类
- 《零基础入门学习Python》学习过程笔记【32,33,34异常处理】(没看)
- python学习笔记(六)文件夹遍历,异常处理