异常类型解说的实例

using System;

namespace 异常
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Test();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                Console.WriteLine(exception.InnerException.Message);
                MyException ex = exception.InnerException as MyException;
                Console.WriteLine(ex.Name);
                Console.WriteLine(ex.Age);


            }
            Console.ReadKey();
           
        }
        static void Test()
        {
            int a = 3;
            int b = 0;

            int c;
            try
            {
                c = a / b;
            }
            catch (DivideByZeroException ex)
            {
                MyException myException = new MyException { Name = "zhangsan", Age = "24" };
                throw new Exception("xxxxx", myException);
            }
        }
    }

    public class MyException : ApplicationException
    {
        public string Name { get; set; }
        public string Age { get; set; }

        public MyException(string msg)
            : base(msg)
        {

        }

        public MyException()
        {
        }
    }

    internal class OverRnageException :ApplicationException
    {
        public OverRnageException(string spyName)
            : base("不是一个有效数字: " + spyName)
        {

        }
    }
}

 

posted @ 2013-01-29 11:50  天之涯,海之角  阅读(116)  评论(0编辑  收藏  举报