博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Custom exception in C#
阅读量:6692 次
发布时间:2019-06-25

本文共 2352 字,大约阅读时间需要 7 分钟。

Although the .net framework contains all kinds of exception types which are sufficient in most cases, it can make sense to define custom exception in our own application. They can greatly simplify and imprve the error handling.

Custom exception derive from Exception calss.

Define custom exception calss:

 

// define one base class derive from Exception    public class DataLayerException : Exception    {        public DataLayerException()            : base()        {         }        public DataLayerException(string message)            : base(message)        {         }        public DataLayerException(string message, Exception innerexception)        {         }    }    // define a child class derive from base calss    public class FileNotFoundException:DataLayerException    {        public FileNotFoundException() : base()        {        }        public FileNotFoundException(string filename)            : base(filename)        {         }        public FileNotFoundException(string filename, Exception innerexception)            : base(filename, innerexception)        {         }        public FileNotFoundException(string format, params object[] parmeter)            : base(string.Format(format, parmeter))        {         }        public FileNotFoundException(string format, Exception innerexception, params object[] parmeter)            : base(string.Format(format, parmeter), innerexception)        {         }        public FileNotFoundException(SerializationInfo info, StreamingContext context)            : base(info, context)        {         }

 1. Throw exception with out message

throw new FileNotFoundException()

 2. Throw exception with simple message

throw new FileNotFoundException(message)

 3. Throw exception with message format and parameters

throw new FileNotFoundException("Exception with parameter value '{0}'", param)

4. Throw exception with simple message and inner exception

throw new FileNotFoundException(message, innerException)

 5. Throw exception with message format and inner exception. Note that, the variable length params are always floating.

throw new FileNotFoundException("Exception with parameter value '{0}'", innerException, param)

6. The last flavor of custom exception constructor is used during exception serialization/deserialization.

 

 

 

转载于:https://www.cnblogs.com/Jenny90/archive/2013/03/19/2969242.html

你可能感兴趣的文章
利用js_API 运行对html文档元素的属性的CRUD操作
查看>>
Linux 架构
查看>>
数据类型 text 和 varchar 在 add 运算符中不兼容
查看>>
查询任务计划
查看>>
IOS--UISwitch的使用方法
查看>>
Spiral Matrix
查看>>
wikioi 1080 线段树练习 树状数组
查看>>
ArcGIS查找空洞多边形
查看>>
[翻译] JTNumberScrollAnimatedView
查看>>
2015年开局
查看>>
开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo(转)...
查看>>
还没被玩坏的robobrowser(8)——robobrowser的实现原理
查看>>
怎么学习逆向工程?
查看>>
D3DXMatrixMultiply 函数
查看>>
[翻译] ZLSwipeableView
查看>>
PHP操作MongoDB 数据库
查看>>
Quartz.Net的使用(简单配置方法)定时任务框架
查看>>
xss编码小结
查看>>
linux grep命令详解
查看>>
胡思乱想 & 胡言乱语
查看>>