分享

V7.3预定义异常类

 时间剧毒 2019-04-29

using System;

using System.Collections.Generic;

using System.Text;

namespace V7._预定义异常类

{

    class Program

    {

        static int DivdeByTwo(int num) {

            if (num % 2 == 1) throw new ArgumentException("此处必须为偶数", "num");

            return num / 2;

        }

        static void Main(string[] args)

        {

            //IndexOutOfRangeException 当一个数组的下标超出范围时运行引发

            int[] myint ={1,2,3,4,5,6,7,8,9,0 };

            try

            {

                for (int i = 0; i < 11; i++)

                {

                    Console.WriteLine(myint[i]);

                }

            }

            catch (IndexOutOfRangeException exIOR) {    

                Console.WriteLine(exIOR.Message.ToString());

            }

            // NullReferenceException 当一个空对象被引用时运行发生

            try {

                string mystr = null;

                Console.WriteLine(mystr.ToString());

            }

            catch(NullReferenceException  ex) {

                Console.WriteLine(ex.Message.ToString());

            }

            //ArgumentException 所有参数异常的基类

            try

            {

                Console.WriteLine(DivdeByTwo(9));

            }

            catch (ArgumentException ex) {

                Console.WriteLine(ex.Message.ToString());

            }

            Console.ReadKey();

        }

    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多