Lession13 调试和异常处理

调试和异常处理1

  • List itemtry:一个 try 块标识了一个将被激活的特定的异常的代码块。后跟一个或多个 catch 块。
  • catch:程序通过异常处理程序捕获异常。catch 关键字表示异常的捕获。
  • finally:finally 块用于执行给定的语句,不管异常是否被抛出都会执行。例如,如果您打开一个文件,不管是否出现异常文件都要被关闭。
  • throw:当问题出现时,程序抛出一个异常。使用 throw 关键字来完成。

/*
* 语法上没有错误,在程序运行的过程中,由于某些原因程序出现错误,而不能正常运行
* 在代码中应该经常使用try-catch 来进行异常捕获
*
* try-catch之间不允许有其他代码
* try{
* 可能会出现异常的代码
* }
* catch{
* 出现异常后要执行的代码
* }
* finally{
* 有没有异常都是执行的代码
* }
* 执行过程:如果try中的代码没有出现异常,那么catch中代码不会执行
* 如果try中的代码出现了异常,哪怕这行代码后面有100行,都不会执行
* 而是直接跳到catch中执行代码
*
*/



using System;

namespace 调试和异常处理
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 0;
            Console.WriteLine("请输入一个数字:");
            try
            {
                //有可能出现的异常
                number = int.Parse(Console.ReadLine());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //Console.WriteLine("您输入的类型不对,需要输入纯数字!");
            }
            finally
            {
                Console.WriteLine("#####################################");
            }

            Console.WriteLine(number);
            Console.ReadKey();

        }
    }
}


在这里插入图片描述

调试

/*
* 程序调试:
* 1.写完一段程序后,想看一下这段程序的执行过程
* 2.当你写完这段程序后发现程序并按照你所想的执行
* 调试方法:断点调试
* F11:逐语句调试(单步调试)
* F10:逐过程调试
*
* 局部变量:显示当前正在运行的方法中的局部变量的值
* 步骤:开启断点->运行->调试菜单栏->窗口->局部变量 F11
*
* 监视:开启断点->运行->调试菜单栏->窗口->监视 (要输入监视的变量名称)F11
* 即时:运行->全部中断->窗口->即时:要查找的值 ,必须在变量前面加“?” 按回车键 出结果
* 快速监视:
*
*/

using System;

namespace 调试
{
    class Program
    {
        static void Main(string[] args)
        {
            int r = Add(1, 2);
            Console.ReadKey();
        }

        public static int Add(int a, int b)
        {
            return a + b;
        }
    }
}

catch块的几种写法

using System;

namespace catch块的几种写法
{
    //DivideByZeroException
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int n = 10, m = 0;
                int r = n / m;
                Console.WriteLine(r);
            }
            //第三种
            catch (DivideByZeroException e)
            {
                Console.WriteLine("除数为0的异常:{0}", e.Message);
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine("空指针异常:详细信息{0}", e.Source);
            }
            catch (FormatException e)
            {
                Console.WriteLine("格式化异常,详细信息{0}", e.StackTrace);
            }
            //这个一定要写在最后
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            //第一种写法
            //catch 
            //{
            //    Console.WriteLine("除数不能为0");

            //}
            //第二种
            //catch (DivideByZeroException e)
            //{
            //    Console.WriteLine(e.Message);//发生的异常信息
            //    Console.WriteLine(e.Source);//导致发生异常的应用程序或者对象名称或者命名空间
            //    Console.WriteLine(e.StackTrace);//发生异常的跟踪信息:哪个文件,第几行,哪个代码
            //}

            Console.ReadKey();
        }
    }
}

在这里插入图片描述

思考题

using System;

namespace 思考题
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("1111111111111111111111111111111111111");
                int n = 10, m = 0;
                int r = n / m;
                Console.WriteLine("2222222222222222222222222222222222222");
                return;
            }
            catch (Exception)
            {

                Console.WriteLine("33333333333333333333333333333333333");
            }
            finally
            {
                Console.WriteLine("4444444444444444444444444444444444444444");
            }
            Console.WriteLine("55555555555555555555555555555555555555555555");
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

应用程序实例

using System;

namespace 应用程序实例
{
    class Program
    {
        //检查邮箱是否合法
        public static bool CheckEmail(string email)
        {
            string[] arrString = email.Split("@");
            //如果输入的邮箱不能被@分割成两部分的话则抛出异常
            if (arrString.Length != 2)
            {
                throw new EmailErrorException("电子邮箱@符错误");
            }
            else
            {
                //找邮箱中.的位置
                int index = arrString[1].IndexOf(".");
                //如果点是@后的第一位或找不到点或最后一位的话
                if (index == 0 || index == -1 || index == arrString[1].Length - 1)
                {
                    throw new EmailErrorException("电子邮箱点号错误");
                }
            }
            return true;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个正确的邮箱地址:");
            string email = Console.ReadLine();
            if (email.Length == 0)
            {
                Console.WriteLine("请不要输入空信息");
                return;
            }

            try
            {
                CheckEmail(email);
            }
            catch (EmailErrorException e)
            {
                Console.WriteLine("格式错误:{0}", e.Message);
            }

            Console.WriteLine($"您输入的邮箱地址是{email}");
            Console.ReadKey();

        }
    }

    public class EmailErrorException : Exception
    {
        public EmailErrorException(string message) : base(message)
        {

        }
    }
}

在这里插入图片描述

自行引发的异常throw

using System;

namespace 应用程序实例
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请输入一个人名:");
                string name = Console.ReadLine();
                if (name == "屈增辉")
                {
                    //手动抛出的异常
                    throw new MyException("不能输入这个名字");
                }
                else
                {
                    Console.WriteLine("可以输入的名字是{0}", name);
                }
            }
            catch (MyException e)
            {
                Console.WriteLine("异常发生啦");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            Console.ReadKey();
        }
    }

    //自定义异常类
    public class MyException : Exception
    {
        public MyException(string message) : base(message) { }
    }
}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞鹰@四海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值