数据库的表中有外键约束,在删除外键时会抛出异常,现在想把这个异常专门提出来告诉用户(给出提示信息),
问题:即如果是因删除外键而导致的异常将给出提示信息,其他异常则有公共异常给出。
希望各位大侠给出例子,在线等。

解决方案 »

  1.   


     public class myExecption :Execption
    {
       自己写
    }
      

  2.   


     public class FilesException:ApplicationException
        {
            #region ¹¹Ô캯Êý
            /// <summary>
            /// Ä¬ÈϹ¹Ô캯Êý
            /// </summary>
            public FilesException()
            { }
            /// <summary>
            /// ´øÓÐÒì³£ÐÅÏ¢µÄ¹¹Ô캯Êý
            /// </summary>
            /// <param name="message">¸½¼ÓÐÅÏ¢</param>
            public FilesException(string message)
                : base(message)
            { }        /// <summary>
            /// ´øÓÐÒì³£ÐÅÏ¢,ÄÚ²¿Òì³£¶ÔÏóµÄ¹¹Ô캯Êý
            /// </summary>
            /// <param name="message">Òì³£ÐÅÏ¢</param>
            /// <param name="innerException">ÄÚ²¿Òì³£¶ÔÏó</param>
            public FilesException(string message, System.Exception innerException)
                : base(message, innerException)
            { }
            /// <summary>
            /// ´øÓÐÐòÁл¯ºÍÁ÷µÄ¹¹Ô캯Êý
            /// </summary>
            /// <param name="info"></param>
            /// <param name="context"></param>
            public FilesException(SerializationInfo info, StreamingContext context)
               : base(info, context)
            { }
            #endregion        #region Òì³£·½·¨        /// <summary>
            /// ·Ç·¨²Ù×÷Òì³£
            /// </summary>
            /// <param name="message">Òì³£ÐÅÏ¢</param>
            /// <returns>Òì³£¶ÔÏó</returns>
           internal static  FilesException InvalidOperation(string message)
            {
                return new FilesException("·Ç·¨²Ù×÷£º" + message);
            }
            /// <summary>
           /// ÎÞЧ²ÎÊýÒì³£
            /// </summary>
           /// <param name="message">Òì³£ÐÅÏ¢</param>
           /// <returns>Òì³£¶ÔÏó</returns>
           internal static  FilesException InvalidArgument(string message)
            {
                return new FilesException(message);
            }
            /// <summary>
            /// Êý¾Ý¿â²Ù×÷Òì³£
            /// </summary>
            /// <param name="message">Òì³£ÐÅÏ¢</param>
            /// <returns>Òì³£¶ÔÏó</returns>
           internal static  FilesException DataBaseError(string message)
            {
                return new FilesException("Êý¾Ý¿âÒì³££º" + message);
            }        /// <summary>
            /// ¶ÔÏó²»´æÔÚÒì³£
            /// </summary>
            /// <param name="message">Òì³£ÐÅÏ¢</param>
            /// <returns>Òì³£¶ÔÏó</returns>
           internal static  FilesException ObjectIsNotExist(string message)
            {
                return new FilesException("¶ÔÏó²»´æÔÚ£º" + message);
            }
           /// <summary>
           /// ¶ÔÏóÒÔÒÑ´æÔÚÒì³£
           /// </summary>
           /// <param name="message">Òì³£ÐÅÏ¢</param>
           /// <returns>Òì³£¶ÔÏó</returns>
           internal static  FilesException ObjectIsExist(string message)
            {
                return new FilesException("¶ÔÏóÒÑ´æÔÚ£º" + message);
            }
            /// <summary>
            /// ÎÞЧµÄ¶ÔÏóÀàÐÍÒì³£
            /// </summary>
            /// <returns></returns>
            internal static FilesException InvalidObjectType(FilesObjectType type)
            {
                throw new FilesException(string.Format("¸Ãº¯Êý²»Ö§³Ö{0}ÀàÐͶÔÏó", type));
            }        #endregion
        }
      

  3.   

        public class FilesException:ApplicationException
        {
            #region 构造函数
            /// <summary>
            /// 默认构造函数
            /// </summary>
            public FilesException()
            { }
            /// <summary>
            /// 带有异常信息的构造函数
            /// </summary>
            /// <param name="message">附加信息</param>
            public FilesException(string message)
                : base(message)
            { }        /// <summary>
            /// 带有异常信息,内部异常对象的构造函数
            /// </summary>
            /// <param name="message">异常信息</param>
            /// <param name="innerException">内部异常对象</param>
            public FilesException(string message, System.Exception innerException)
                : base(message, innerException)
            { }
            /// <summary>
            /// 带有序列化和流的构造函数
            /// </summary>
            /// <param name="info"></param>
            /// <param name="context"></param>
            public FilesException(SerializationInfo info, StreamingContext context)
               : base(info, context)
            { }
            #endregion        #region 异常方法        /// <summary>
            /// 非法操作异常
            /// </summary>
            /// <param name="message">异常信息</param>
            /// <returns>异常对象</returns>
           internal static  FilesException InvalidOperation(string message)
            {
                return new FilesException("非法操作:" + message);
            }
            /// <summary>
           /// 无效参数异常
            /// </summary>
           /// <param name="message">异常信息</param>
           /// <returns>异常对象</returns>
           internal static  FilesException InvalidArgument(string message)
            {
                return new FilesException(message);
            }
            /// <summary>
            /// 数据库操作异常
            /// </summary>
            /// <param name="message">异常信息</param>
            /// <returns>异常对象</returns>
           internal static  FilesException DataBaseError(string message)
            {
                return new FilesException("数据库异常:" + message);
            }        /// <summary>
            /// 对象不存在异常
            /// </summary>
            /// <param name="message">异常信息</param>
            /// <returns>异常对象</returns>
           internal static  FilesException ObjectIsNotExist(string message)
            {
                return new FilesException("对象不存在:" + message);
            }
           /// <summary>
           /// 对象以已存在异常
           /// </summary>
           /// <param name="message">异常信息</param>
           /// <returns>异常对象</returns>
           internal static  FilesException ObjectIsExist(string message)
            {
                return new FilesException("对象已存在:" + message);
            }
            /// <summary>
            /// 无效的对象类型异常
            /// </summary>
            /// <returns></returns>
            internal static FilesException InvalidObjectType(FilesObjectType type)
            {
                throw new FilesException(string.Format("该函数不支持{0}类型对象", type));
            }        #endregion
        }
      

  4.   

    唉,楼上贴那么多辛苦了。可是哪一行是针对lz的程序流程来解决的呢?捕获SQL抛出的异常我不太确定,因为我这些年从来不在产品中使用SQL Server之类的收费数据库。捕获基本上是这样的:    try
        {
            //删除你的数据吧
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            if (ex.Number == 547)
                throw new Exception("这是我包装的新的Exception,哈哈。", ex);
            else
                throw ex;
        }
    }
      

  5.   

    实际上,应用程序往往会自定自己的Exception子类,然后抛出自定义的Excepion对象实例。而不是像上面我写的 thow new Exception......。不过我提醒的是,不论你自己定义的Exception子类怎样设计,使用时都要像我写的那样允许把原来的ex作为InnerException参数。切记!这是规矩。