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

namespace learn_try
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 2;
            try
            {
                // 自定义一个异常
                throw new Exception("123");
                //手动抛出异常
                throw new DivideByZeroException();
                //try可以捕捉异常
                int b = 2 / (a - a);
                Console.WriteLine("123");
            }
            // 捕捉指定的异常,后可以加when语句,表示只有当条件满足时才生效
            catch (DivideByZeroException) when (1 == 1)
            {
                Console.WriteLine("0 catnt be Divide");
            }
            // 可以有多个catch块,捕捉不同的异常进行处理
            catch (Exception ex)
            {
                Console.WriteLine($"an error: {ex}");
            }
            finally
            {
                Console.WriteLine("end");
            }
            Console.ReadKey();
        }
    }
}
posted on 2023-01-19 12:40  盈盈的月儿  阅读(12)  评论(0编辑  收藏  举报