自定义异常类

题目描述   

自定义异常类。(控制台应用程序)

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

namespace 自定义异常类
{
    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());
            }

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

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


猜你喜欢

转载自blog.csdn.net/WYJ____/article/details/80279830