异常--throw

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

namespace @throw
{
    class test
    {
        public int Myint(string a, string b)
        {
            int intn1;
            int intn2;
            int num;
            intn1 = int.Parse(a);
            intn2 = int.Parse(b);
            if(intn2 == 0)
            {
                throw new DivideByZeroException();
                return 0;
            }
            else
            {
                num = intn1 / intn2;
                return num;
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            while(true)
            {
                Console.WriteLine("请输入分子:");
                string str1 = Console.ReadLine();
                Console.WriteLine("请输入分母:");
                string str2 = Console.ReadLine();
                test tt = new test();
                Console.WriteLine(tt.Myint(str1,str2));
            }
        }
    }
}

  throw在特定情况下主动抛出异常

(1)ArithmeticException

(2)ArrayTypeMismatchException

(3)DivideByZeroException

(4)IndexOutOfRangeException

(5)InvalidCastException

(6)NullReferenceException

(7)OutOfMemoryException

(8)OverflowException

(9)StackOverflowException

(10)TypeInitializationException

原文地址:https://www.cnblogs.com/my-cat/p/7228729.html