博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 自定义异常的方法源码演示及说明
阅读量:4628 次
发布时间:2019-06-09

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

内容之余,把做工程过程中较好的内容段备份一下,下边内容是关于C# 自定义异常的方法演示及说明的内容,希望能对各位朋友有一些好处。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication3
{
{
public PayOverflowException() { }
public PayOverflowException(string message)
: base(message) { }
public PayOverflowException(string message, Exception inner)
: base(message, inner) { }
}
internal class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int CurrPay { get; set; }
public Employee() { }
public Employee(int id, string name, int currpay)
{
this.ID = id;
this.Name = name;
this.CurrPay = currpay;
}
public virtual void GiveBunus(int amount)
{
var pay = CurrPay;
this.CurrPay += amount;
if (CurrPay > 10000)
{
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)
{
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();
}
catch (Exception ex1)
{
var inner = new PayOverflowException(ex.Message, ex1);
throw inner;
}
}
}
}
}

值得注意的是:在实例化的时候调用的是PayOverflowException(stringmessage,Exceptioninner)构造函数,如果本程序如果有其他程序在调用的时候,可以通过.InnerExcetpion的Message属性进行查看内部异常。

 

转载于:https://www.cnblogs.com/odsxe/p/10111286.html

你可能感兴趣的文章
我不为人人,人人不为我
查看>>
iOS网络编程(三) 异步加载及缓存图片---->SDWebImage
查看>>
Qt qml 模拟iphone slide to unlock 的聚光动画文字效果
查看>>
c++11 std::move() 的使用
查看>>
HDU 4607 Park Visit (DP最长链)
查看>>
实例学架构设计之源起复杂度
查看>>
leetcode- Rotate Array 旋转数组
查看>>
vue vuex
查看>>
POJ 2234 Matches Game 博弈论水题 Nim模型
查看>>
BBC-unit6 session4
查看>>
JS获取节点的兄弟,父级,子级元素的方法(js获取子级获取到换行与空格元素-FF)...
查看>>
ini文件操作
查看>>
Win7 本地打印后台处理程序服务没有运 怎么办
查看>>
php 中array_multisort排序,类似于对数据库中的记录依次按多列进行排序
查看>>
加密算法和MD5等散列算法的区别
查看>>
【python】函数返回值
查看>>
Java基础50题test9—求完数
查看>>
【记忆法】心智绘图
查看>>
Unable to create request (bad url?) 解决方案
查看>>
网络对抗技术_实验三_密码破解技术
查看>>