第十一章调试与异常

TomHanck4
2022-06-13 / 0 评论 / 0 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年06月15日,已超过1039天没有更新,若内容或图片失效,请留言反馈。

第十一章调试与异常

目标

  • 理解如何调试应用程序和排除错误
  • 掌握如何测试C#应用程序
  • 在程序中进行错误捕获和错误处理

调试

  • 必要性

    • 在不熟应用程序前必须先对其进行调试
    • 在事物处理过程中,系统显示错误信息
  • 错误类型

    • 语法错误
    • 逻辑错误
    • 运行时错误

调试过程

  • 观察程序运行时行为
  • 跟踪变量的值
  • 确定语义错误的位置
  • VS中的操作步骤

    • 合理设置断点

      • 鼠标右键
      • F9
    • 运行Debug

      • F5
      • F10单步执行

VS.NET调试工具

  • “局部变量”窗口
  • “监视”窗口
  • “即时”窗口
  • “快速监视”窗口

异常

  • C#中的异常处理语句

    • 异常是由try语句来处理的
    • try语句提供了一种机制来捕捉块执行过程中发生的异常
  • C提供了3种异常处理结构:

    • try-catch
    • try-catch-finally
    • try-finally
  • try块:

    • try块监视可能抛出异常的语句。
      try
      {
      //statements that may cause an exception
      }

·try块管理包含在它内部的语句,定义与它相关的异常处理程序的范围。
·try块必须至少有一个catch块。

  • C#引发异常有以下两种方式

    • 使用显式throwi语句来引发异常控制权无条件转到处理异常的部分代码
    • 使用语句或表达式在执行过程中激发了某个异常的条件,使得操作无法正常结束,从而引发异常

      • Try......Catch......Finally
        Try catch异常
  • I/O设备异常
   try
   {
    //程序代码
   }
   catch(IOException E)
   {
   //错误处理代码
   }
  • System.Exception可以是系统中任何一种异常

Try/catch/finally异常

  • Catch可以多重
  • Finally块

    • 一般清理内存用
    • Try块中最后执行

System.Exception

属性含义
Message描述当前异常对象的字符串
Source引必当前异常对象的程序或对象名称
StackTrace发异常时调用堆栈上的帧的字符串表示
InnerException表示引发当前异常的内部异常对象

常用异常类

  • ArithmeticException数学运算、类型转换异常类
  • InvalidCastException不正确转型异常。例如String转Decimal操作。
  • IndexOutOfRangeException数组访问越界异常。
  • NullReferenceException空引用对象异常。

实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
    
namespace Lesson11_2
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            try
            {
                //程序尝试执行的代码
                list.Add(0);
                list.Add(1);
                list.Add("2");
                list.Add(3);
                list.Add(4);

                Console.WriteLine(list[7]);

                foreach (int item in list)
                {
                    Console.WriteLine(item);
                }
            }
            catch (IndexOutOfRangeException indexOut)//下标越界异常
            {
                Console.WriteLine(indexOut.Message); 
            }
            catch (Exception ex)//想要捕获的异常类型
            {
                //错误处理代码
                Console.WriteLine(ex.Message);//输出异常的信息
            }
            finally
            {
                //不管有没有出现异常或者异常有没有被处理,都会最终执行的代码
                Console.WriteLine("程序终止!");
            }
            

        }
    }
}

Throw语句

Throw语句

自定义异常

  • 当开发人员需要提供更为广泛的信息,或需要程序具有特殊功能时,就可以定义自定义异常。首先要创建一个自定义异常类,该类必须直接或间接派生自System.ApplicationException。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson11_3
{
    /// <summary>
    /// 除数不能为0为异常
    /// </summary>
    class MyCustomException:ApplicationException
    {
        public MyCustomException()
        {

        }
        public MyCustomException(string message)
                : base(message)
        {

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

namespace Lesson11_3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你的除数");
            int divisor = int.Parse(Console.ReadLine());

            try
            {
                if (divisor==0)
                {
                    throw new MyCustomException("除数不能为0");
                }

            }
            catch (MyCustomException ex)
            {

                //throw ex;
                Console.WriteLine("自定义异常信息"+ex.Message);
            }
        }
    }
}

注意事项

  • 请勿将try/catch块处于控制流。
  • 用户只能处理catch异常。
  • 不得声明空catch块。
  • 避免在catch块内嵌套try/catch
  • 只有使用finally块才能从try语句中释放资源。

总结

  • 调试

    • 搜寻和消除应用程序中的错误的过程。
    • 语法错误表示编译器无法理解代码。
    • “局部变量”窗口允许用户监控当前方法中所有局部变量的值。
  • 异常

    • 当应用程序遇到运行时错误,就会引发异常。
    • C#中的所有异常都派生自System.Exception类。
0

评论 (0)

取消