zoukankan      html  css  js  c++  java
  • C# 异常机制

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    namespace ConsoleApplication3
    {
        [Serializable] //声明为可序列化的 因为要写入文件中
        public class PayOverflowException : ApplicationException//由用户程序引发,用于派生自定义的异常类型
        {
            /// <summary>
            /// 默认构造函数
            /// </summary>
            public PayOverflowException() { }
            public PayOverflowException(string message)
                : base(message) { }
            public PayOverflowException(string message, Exception inner)
                : base(message, inner) { }
            //public PayOverflowException(System.Runtime.Serialization.SerializationInfo info,
            //    System.Runtime.Serialization.StreamingContext context)
            //    : base(info, context) { }
        }

        internal class Employee
        {
            public int ID { get; set; }
            public string Name { get; set; }
            /// <summary>
            /// current pay
            /// </summary>
            public int CurrPay { get; set; }

            public Employee() { }
            public Employee(int id, string name, int currpay)
            {
                this.ID = id;
                this.Name = name;
                this.CurrPay = currpay;
            }

            /// <summary>
            /// 定义一个GiveBunus的虚方法以供不同的派生类进行重载
            /// </summary>
            /// <param name="amount">奖金额度</param>
            public virtual void GiveBunus(int amount)
            {
                //用一个临时变量记录递增之前的值
                var pay = CurrPay;

                this.CurrPay += amount;

                if (CurrPay > 10000)
                {
                    //发生异常,将CurrPay的值进行恢复,
                    //并抛出异常,外部程序捕获次异常
                    this.CurrPay = pay;
                    var ex = new PayOverflowException("The employee's max pay should be no more than 10000.");
                    throw ex;
                }
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("**** 创建Employee对象,并用try/catch捕获异常 *****");

                var emp = new Employee(10001, "Yilly", 8000);
                try
                {
                    emp.GiveBunus(3000);
                }
                catch (PayOverflowException ex)
                {
                    Console.WriteLine("异常信息:{0}\n发生于{1}类的{2}方法", ex.Message,
                        ex.TargetSite.DeclaringType, ex.TargetSite.Name);

                    try
                    {
                        var file = new FileStream(@"c:\customerexception.txt", FileMode.Create);
                        //*** 异常信息写入文件中的代码省略...
                        //以序列化方式写入
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(file, ex);
                        file.Close();

                        //以字节方式写入
                        //byte[] buffer = System.Text.Encoding.Default.GetBytes(ex.Message);
                        //int leng = 0;
                        //leng = buffer.GetLength(0);
                        //file.Write(buffer, 0, leng);
                        //file.Close();
                    }
                    catch (Exception ex1)
                    {
                        var inner = new PayOverflowException(ex.Message, ex1);
                        throw inner;
                    }
                }

            }
        }
    }

  • 相关阅读:
    【MVC整理】1.使用 StructureMap 作为 ASP.NET MVC 的 DI 框架
    一个随机列表项算法
    轻量级IOC框架:Ninject
    解决“找不到请求的 .Net Framework 数据提供程序。可能没有安装”的问题
    Vcastr 2.2 flv 网络播放器 参数设置
    C#取汉字首字母
    IIS Express的安装与设置讲解
    清晰的图片缩略方案
    ssh远程连接ubuntu
    【Apache】在Apache中利用ServerAlias设置虚拟主机接收多个域名和设置域名泛解析
  • 原文地址:https://www.cnblogs.com/huanhuan86/p/2735557.html
Copyright © 2011-2022 走看看