C#中自定义异常类

来源:互联网 发布:公安部网络监察局 编辑:程序博客网 时间:2024/06/06 21:42

在C#中,可以自定义异常类,如下所示。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class myException : Exception    {                public myException(string message)            : base(message)        {                     }        public override string Message        {            get            {                   return "自定义异常";            }        }    }    class Program    {        static void Main(string[] args)        {            int[] a={1,2,3,4,5};            int i=0;            try            {                while (i < 100)                {                    Console.Write("{0,2}", a[i]);                    i++;                    if (i > 4)                        throw new myException("自定义异常");                }            }            catch (myException e)            {                Console.WriteLine(e.Message);            }            finally            {                Console.WriteLine("it's over.");            }            Console.ReadLine();        }    }}



0 0
原创粉丝点击