基本信息
源码名称:C# 异常处理常用示例源码
源码大小:0.02M
文件格式:.zip
开发语言:C#
更新时间:2013-02-13
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
除零异常、空引用、自定义异常示例,适合刚入门的同学学习
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } class MyException:ApplicationException { public MyException(String msg):base(msg) { HelpLink = "http://NotARealURL.Microsoft.com/help.html"; } } public void ShowException(System.Exception ex) { string str; str = string.Format("Exception:\n\t{0}\n", ex.GetType().ToString()); str = string.Format("Message:\n\t{0}\n", ex.Message); str = string.Format("Stack Trace:\n\t{0}\n", ex.StackTrace); str = string.Format("Help Link:\n\t{0}\n", ex.HelpLink); MessageBox.Show(str); } // 除以 0 异常。 private void button1_Click(object sender, System.EventArgs e) { int x = 0; try { // 产生异常。 x = 10 / x; } catch(System.Exception ex) { ShowException(ex); } } // 无效对象异常。 private void button2_Click(object sender, System.EventArgs e) { object a = null; try { MessageBox.Show(a.ToString()); } catch(System.Exception ex) { ShowException(ex); } } // 自定义异常。 private void button3_Click(object sender, System.EventArgs e) { try { throw(new MyException("这是我自己定义的异常。")); } catch(System.Exception ex) { ShowException(ex); } }