【原创】C#零基础学习笔记009-异常处理

其他路径:

CSDN: https://blog.csdn.net/wodehao0808

微信公众号:程序喵星人

更多资源和视频教程,QQ:1902686547

9 异常处理

C# 语言的异常处理功能可帮助您处理程序运行时出现的任何意外或异常情况。异常处理使用 try、catch 和 fianlly 关键字尝试某些操作,以处理失败情况,尽管这些操作有可能失败,但如果您确定需要这样做,且希望在事后清理资源,就可以尝试这样做。公共语言运行时(CLR)、.Net Framework 或任何第三方库或者应用程序代码都可以生成异常。异常是使用 throw 关键字创建的。

  使用异常:

    在 C# 中,程序中的运行时错误通过使用一种称为 “异常” 的机制在程序中传播。异常由遇到错误的代码引发,由能够更正错误的代码捕捉。异常可由 .Net FrameWork 公共语言运行时(CLR)或由程序中的代码引发。一旦引发一个异常,这个异常就会在调用堆栈中往上传播,直到找到针对它的 catch 语句。未捕获的异常由系统提供的通用异常处理程序处理,该处理程序会显示一个对话框。

    异常由从 Exception 派生的类表示。此类标识异常的类型,并包含详细描述异常的属性。引发异常涉及到创建一个异常派生类的实例,配置异常的属性(可选),然后使用 throw 关键字引发该对象。

9.1 Example: 异常处理

MyException.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Lesson_37_1

{

    // 自定义的异常,异常的出现时间。

    public class MyException : Exception

    {

        private DateTime _dt;

        public DateTime Dt

        {

            get { return _dt; }

            set { _dt = value; }

        }

        private string _codeNum;

        public string CodeNum

        {

            get { return _codeNum; }

            set { _codeNum = value; }

        }

        public MyException(string p_codeNum, Exception p_ex)

            : base( p_ex.Message )

        {

            CodeNum = p_codeNum;

            Dt = DateTime.Now;

        }

        public void showExceptionMsg()

        {

            Console.WriteLine("异常出现时间:" + Dt + ", 出现异常的代码:" + CodeNum + ", 异常的信息:" + Message);

        }

    }

}

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

// 异常处理

namespace Lesson_37_1

{

    class Program

    {

        // 异常出现的时间是在程序运行的时候,程序运行的时候的错误那么就需要进行异常处理

        // 所有的异常都是从 Exception 中继承的

        static void Main(string[] args)

        {

            /*

            // example_1

            Console.WriteLine("请输入一个数字");

            try  // 检测有可能出现的异常代码,有 try 必须就有 catch

            {

                int intGetNum = Convert.ToInt32(Console.ReadLine());

            }

            catch (Exception ex)

            {

                Console.WriteLine("程序出现异常,异常信息:" + ex.Message);

            }

            finally  // finally 可写也可以不写,无论是否出现异常,finally里的程序代码都会执行

            {

                Console.WriteLine("这是 finally 执行语句");

            }

            */

            // example_2: 自定义异常处理类

            WriteMsg();

        }

        static void WriteMsg()

        {

            Console.WriteLine("请输入一个数字");

            try  // 检测有可能出现的异常代码,有 try 必须就有 catch

            {

                int intGetNum = Convert.ToInt32(Console.ReadLine());

            }

            catch (Exception ex)

            {

                MyException myEx = new MyException("20", ex);

                myEx.showExceptionMsg();

            }

            finally  // finally 可写也可以不写,无论是否出现异常,finally里的程序代码都会执行

            {

                Console.WriteLine("这是 finally 执行语句");

            }

        }

    }

}

原文地址:https://www.cnblogs.com/wodehao0808/p/14608141.html