预定义异常类(2)

题目描述   

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

异常类 说明
InvalidCastException 类型的显示转换在运行失败时,就会引发此异常
ArrayTypeMismatchException 当存储一个数组时,如果由于被存储的元素的实际类型与数组的实际类型不兼容而导致存储失败,就会引发此异常
ArithmeticException 算数运算期间异常引发的基类
DivideByZeroException 试图除以零引发异常
OverflowException 溢出时引发
FormatException 参数格式无效时引发
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 预定义异常类
{
    class Program
    {
        static void Main(string[] args)
        {
            bool mybool = true;
            try
            {
                char mychar = Convert.ToChar(mybool);
                Console.WriteLine(mychar);
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}  

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

namespace 预定义异常类
{
    class Program
    {
        static void Main(string[] args)
        {
            string [] mystr = { "Cat", "Dog", "Pig" };
            object[] myobj = mystr;
            try
            {
                foreach (object outboj in myobj)
                {
                    Console.WriteLine(outboj);
                    Console.WriteLine(outboj.GetType());
                    myobj[2] = 13;
                }
            }
            catch (ArrayTypeMismatchException ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wyj____/article/details/80272643