首页 > 其他 > 详细

定制异常

时间:2019-02-08 10:08:39      阅读:177      评论:0      收藏:0      [点我收藏+]
 
技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 自己写的
 7 {
 8     public class dingzhiyichang : Exception
 9     {
10         private int i1;
11         public int I1
12         {
13             get
14             {
15                 return i1;
16             }
17         }
18         public dingzhiyichang(int i2) : base("定制的异常消息?")
19         {
20             i1 = i2;
21         }
22 
23     } //扩展了下基类的事件处理程序
24 
25     class Program
26     {
27 
28 
29         static int getint(int i_3)
30         {
31             int[] i_4 = new int[3] { 1, 2, 3 };
32             if (i_3>=0&&i_3<=2)
33             {
34                 Console.WriteLine(i_4[i_3]);
35                return i_3;
36                
37             }
38             else
39             {
40                 throw new dingzhiyichang(i_3);
41             }
42         } //随便写了一个方法
43 
44 
45 
46         static void Main(string[] args)
47         {
48             
49             for (int i_5 = 0; i_5 < 6; i_5++)
50             {
51 
52                 try
53                 {
54                     getint(i_5);//尝试调用写的那个方法
55                 }
56                 catch (dingzhiyichang e)//如果调用出错,就显示自定义的错误消息
57 
58                 { 
59                 //throw;
60                 Console.WriteLine(e.Message);
61                 }
62 
63             }
64             Console.ReadLine();
65         }
66     }
67 }
定义了一个异常消息,并调用

 

不明白为啥要整这么复杂,使用if else貌似也可以,先写这里记着吧,回头可能用到

定制异常

原文:https://www.cnblogs.com/myjobok/p/10355806.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!