zoukankan      html  css  js  c++  java
  • Custom exception in C#

    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.

  • 相关阅读:
    剑指 Offer 06. 从尾到头打印链表
    剑指 Offer 05. 替换空格
    剑指 Offer 04. 二维数组中的查找
    剑指 Offer 03. 数组中重复的数字
    leetcode刷题笔记328题 奇偶链表
    leetcode刷题笔记324题 摆动排序 II
    leetcode刷题笔记321题 拼接最大数
    leetcode刷题笔记326题 3的幂
    20210301日报
    20210227日报
  • 原文地址:https://www.cnblogs.com/Jenny90/p/2969242.html
Copyright © 2011-2022 走看看