C#11处理程序异常相关技术

捕获异常

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

例子: 访问数组,防止越界

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._1捕获异常
{
    class Program
    {
        static void Main(string[] args)
        {
            //利用try  catch 语句来捕获数组的越界问题
            int[] myint = { 0, 2, 4, 6, 8, 10, 12, 16, 18, 20 };
            try
            {
                for(int i=0;i<=myint.Length;i++)
                {
                    Console.Write(myint[i].ToString() + " ");
                }
            }

            catch(Exception myex)
            {
                Console.WriteLine(myex.Message.ToString());
            }
            
            
            
            // 词条语句虽然可以捕获异常并给出提示,但并不能只能的描述异常的原因
            //catch
            //{
            //    Console.WriteLine("异常已经发生");
            //}

            Console.ReadKey();
        }
    }
}

结果:

0 2 4 6 8 10 12 16 18 20 索引超出了数组界限。

清除、处理所有异常

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._2清除捕获异常
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myint = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
            try
            {
                for (int i = 0; i < myint.Length; i++)
                    Console.Write("720除以{0}的结果是{1}", myint[i], 720 / myint[i]);


            }
            catch(Exception myex)
            {
                Console.WriteLine(myex.Message.ToString());
            }
            finally
            {
                Console.WriteLine("我什么时候都会执行,无论发生异常与否");
            }
            Console.ReadKey();
        }
    }
}

结果:

尝试除以零。
我什么时候都会执行,无论发生异常与否

引发异常

在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._3引发异常
{
    class Program
    {
        //私有方法,只能放在Program中在其他类中时,不能在main方法中进行调用
        private static int ConvertStringToInt(string mystr)
        {
            int outnum = 0;
            try
            {
                outnum = Convert.ToInt32(mystr);
                return outnum;
            }
            catch
            {
                throw new FormatException("格式转换不正确(由我要自学网制作)");
            }
        }

        static void Main(string[] args)
        {
            string mystr = "51zxw";
            try
            {
                int myint;
                myint = Program.ConvertStringToInt(mystr);//初始化
                Console.WriteLine(myint);
            }
            catch(FormatException exf)
            {
                Console.WriteLine(exf.Message.ToString());
            }
            Console.ReadKey();
        }
    }
}

结果:

格式转换不正确(由我要自学网制作)

预定义异常类


在这里插入图片描述

自定义异常类

在这里插入图片描述
自定义的异常类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._6自定义异常类
{
    class MyException:Exception
    {
        public MyException(string message):base(message)
        {

        }
    }
}

主程序调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._6自定义异常类
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("这行代码在引发异常前被执行");
                string mystr = "这是我自己定义的异常,由我要自学网制作";
                throw new MyException(mystr);
                Console.WriteLine("由于引发了异常,这行代码不会被执行");
            }
            catch(MyException ex)
            {
                Console.WriteLine(ex.Message.ToString());


               // Console.WriteLine("这是我自己定义的异常,由我要自学网制作");
            }
            Console.ReadKey();
        }

    }
}

结果:

这行代码在引发异常前被执行
这是我自己定义的异常,由我要自学网制作

本章小结及任务实施

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._7本章小结及任务实施
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入一个整数:");
            try
            {
                int myint = int.Parse(Console.ReadLine());
                double mydouble = 1.0 / myint;
                Console.WriteLine("该数的倒数是:" + mydouble);
            }
            catch(DivideByZeroException)
            {
                Console.WriteLine("产生除零异常");
            }
            catch(OverflowException)
            {
                Console.WriteLine("溢出异常");
            }
            catch(FormatException)
            {
                Console.WriteLine("转换异常");
            }
            catch(Exception)
            {
                Console.WriteLine("其他异常");
            }
            Console.ReadKey();
        }
    }
}

请输入一个整数:589455125696552
溢出异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值