C#的异常处理机制

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace codeTest
{

    class Program
    {
        //C# 语言中的异常处理的基本语法,主要是 try, catch, throw 以及 finally.
        //Exception是所有异常的基类  Exception的性能不好  最好捕抓对应的异常如(NullReferenceException) z
        //这样可以对不同的异常做不同的处理 也可以把未知的异常暴露出来
        static void Main(string[] args)
        {
            try
            {
                int x = 0;
                int y = 100 / x;
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                //不管是否补抓到异常finally里面的代码都会运行
            }



            //手动抛出异常
            throw new NullReferenceException();
            throw new ArgumentException();
            throw new DirectoryNotFoundException();
        }


    }


}
原文地址:https://www.cnblogs.com/lgxlsm/p/3023210.html