自定义Exception

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6.  
  7. namespace Model
  8. {
  9. /// <summary>
  10. /// This class is used to define the custom exception.
  11. /// </summary>
  12. [DataContract]
  13. public class MyExceptionContainer:Exception
  14. {
  15. /// <summary>
  16. /// The exception's starck message.
  17. /// </summary>
  18. [DataMember]
  19. public string ErrorMessage { get; set; }
  20.  
  21. /// <summary>
  22. /// The custom informtion.
  23. /// </summary>
  24. [DataMember]
  25. public string Description { get; set; }
  26.  
  27. #region Constructor
  28.  
  29. public MyExceptionContainer() { }
  30.  
  31. public MyExceptionContainer(string errorMessage, string description)
  32. {
  33. this.ErrorMessage = errorMessage;
  34. this.Description = description;
  35. }
  36.  
  37. #endregion
  38. }
  39. }

UserException

  1. using System;
  2. using System.Runtime.Serialization;
  3. using System.Security.Permissions;
  4.  
  5. namespace Constant
  6. {
  7. /// <summary>
  8. /// The class is defined for const fields of login exception.
  9. /// </summary>
  10. [Serializable]
  11. public class UserException:Exception,ISerializable
  12. {
  13. #region Fields
  14.  
  15. private string message;
  16. private Exception innerException;
  17.  
  18. #endregion
  19.  
  20. #region Constructors
  21.  
  22. public UserException() { }
  23.  
  24. public UserException(string message)
  25. {
  26. this.message = message;
  27. }
  28.  
  29. public UserException(string message, Exception exception)
  30. {
  31. this.message = message;
  32. this.innerException = exception;
  33. }
  34.  
  35. #endregion
  36.  
  37. #region Const fileds of user functions' exception.
  38.  
  39. public const string UserNameIsNull = "*Username is null.";
  40. public const string PasswordIsNull = "*Password is null.";
  41. public const string LoginedFailed = "*Username or password is wrong.";
  42. public const string ChangeNewPasswordIsNull = "*New password is null.";
  43. public const string ChangeConfirmIsNull = "*Confirm is null.";
  44. public const string TwiceEnterIsNotSame = "*New password and confirm is not same.";
  45. public const string PasswordIsWrong = "*Old Password is wrong.";
  46. public const string UpdatePasswordFailed = "The error is come from UpdatePasswordFailed Method in UserDal Class.";
  47. public const string RetrieveUserByUserName = "The error is come from RetrieveUserByUserName Method in UserDal Class.";
  48. public const string ChangePasswordSucceed = "Change password succeed.";
  49. public const string FormatException = "The parameter error.";
  50.  
  51. #endregion
  52. }
  53. }

DAL

  1. public int UpdatePassword(string newPassword, string userName)
  2. {
  3. int influenceNumber = ;
  4.  
  5. try
  6. {
  7. string sqlText = SqlText.UpdatePassword;
  8. SqlParameter[] parms = new SqlParameter[] {
  9. new SqlParameter("@password", newPassword),
  10. new SqlParameter("@userName", userName),
  11. };
  12.  
  13. influenceNumber = SqlHelper.ExecuteNonQuery(sqlText, parms);
  14. }
  15. catch (SqlException ex)
  16. {
  17. throw new UserException(UserException.UpdatePasswordFailed, ex);
  18. }
  19.  
  20. return influenceNumber;
  21. }

BLL

  1. public IList<Exam> RetrieveExamList(string userName, int order)
  2. {
  3. try
  4. {
  5. return examDal.RetrieveExamList(userName, order);
  6. }
  7. catch (ExamException ex)
  8. {
  9. log.Error(ExamException.RetrieveExamList, ex);
  10. throw new FaultException<MyExceptionContainer>( new MyExceptionContainer() {
  11. ErrorMessage = ex.Message,
  12. Description = ExamException.RetrieveExamList
  13. });
  14. }
  15. }
Use
  1. catch (FaultException<MyExceptionContainer> myException)
  2. {
  3. log.Error(myException.Message, myException);
  4. }
  5. catch (FaultException faultException)
  6. {
  7. log.Error(faultException.Message, faultException);
  8. }
  9. catch (Exception exception)
  10. {
  11. log.Error(exception.Message, exception);
  12. }

MyException的更多相关文章

  1. http://www.myexception.cn/program/767123.html

    http://www.myexception.cn/program/767123.html

  2. 31.3 自定义异常类 MyException

    /* * 异常的分类: 运行时期异常:RuntimeException的子类就是运行时期异常,在编译时期可以自由选择处理或者不处理 编译时期异常:是Exception的子类,非RuntimeExcpe ...

  3. PHP验证用户登录例子-学习笔记

    1.基本流程: 2.UML类图: 3.PHP代码: 3.1 index.php <?php /** * Created by PhpStorm. * User: andy * Date: 16- ...

  4. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  5. [C#] C# 知识回顾 - 学会使用异常

    学会使用异常 在 C# 中,程序中在运行时出现的错误,会不断在程序中进行传播,这种机制称为“异常”. 异常通常由错误的代码引发,并由能够更正错误的代码进行 catch. 异常可由 .NET 的 CLR ...

  6. [干货来袭]C#6.0新特性

    微软昨天发布了新的VS 2015 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下也是昨天发布的新的C#6.0的部分新特性吧.. ...

  7. C# BackgroundWorker 详解

    在C#程序中,经常会有一些耗时较长的CPU密集型运算,如果直接在 UI 线程执行这样的运算就会出现UI不响应的问题.解决这类问题的主要途径是使用多线程,启动一个后台线程,把运算操作放在这个后台线程中完 ...

  8. 浅谈Java的throw与throws

    转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...

  9. 札记:Java异常处理

    异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...

随机推荐

  1. BI中PowerDesigner建模

    PowerDesigner下载: http://pan.baidu.com/share/link?shareid=296749&uk=1479845243

  2. CF Destroying Roads (最短路)

    Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. AspNetPager用法

    最近在做数据管理系统的开发工作,遇到数据表中记录过多的问题,虽然asp.net中已有分页控件,但是非常简陋.AspNetPager是一款非常强大的分页控件,它的介绍我这里就不在赘述了,网上有很多它的介 ...

  4. PowerDesigner的数据类型

    用PowerDesigner 15 设计个数据库,每个字段的数据类型设计真是头大,根据字段意思看用哪个类型最合适还得仔细研究呀.贴几个数据类型表格收藏一下^_^ Numeric data types ...

  5. Oracle中的内置函数在sql中的转换整理

    程序里面经常会即支持Oracle数据库,又支持sql数据库.而有些Oracle内置函数用的比较多,但在sql中语法有些不同,我做了些整理,希望可以帮助大家.... 1.oracle中的内置函数:ora ...

  6. 干货:Android 源码使用心得分享

          我相信很多初学者会和我一样经常在网上去找Android开发源码,但是往往因为运行不起来非常的懊恼!在做爱开发网站的时候,收集App代码时就遇到了这种困难,我相信网络上面的源码大部分在发布前 ...

  7. Part 36 to 39 Talking about Delegates in c#

    Part 36 Delegates in c# Part 37 Delegates usage in c# class Progim { public static void Main() { Lis ...

  8. 放弃SCOPE_Identity,使用OUTPUT代替

    最近项目中使用了SCOPE_IDENTITY()来获取新增数据的自动递增ID号. 在运行过程中会不时的发生无法通过SCOPE_IDENTITY()来获取ID号的情况. 尝试着测试又发现不了问题. 今天 ...

  9. shell中使用echo命令改变输出显示样式

    文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例:echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置背景成为蓝色,前景白色,闪 ...

  10. JS闭包理解_摘

    原文地址1:http://www.cnblogs.com/mzwr1982/archive/2012/05/20/2509295.html 闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实 ...