C#异常处理

例:
static void Main(string[] args)
  {
      //
      // TODO: 在此处添加代码以启动应用程序
      //
     Console.WriteLine("请输入被除数:");
     int intFirst=Int32.Parse(Console.ReadLine());
     Console.WriteLine("请输入除数:");
     int intSecond=Int32.Parse(Console.ReadLine());
     try
        {
           if(intSecond==0)
           {
               throw new Exception("除数不能为0.");
            }
           //int intRes=intFirst/intSecond;
           Console.WriteLine("结果:未计算");
         }
         catch(Exception ex)
        {
            Console.WriteLine(ex.Message); 
         }
       Console.WriteLine("异常处理后又执行了这里!");
   }

当有throw这样的抛出异常语句时,一但有异常抛出,这条语句之后的try块的内容将不会再继续被执行,而是从对应catch块之后继续执行,也即是继行Console.WriteLine(ex.Message)这句,而不是执行Console.WriteLine("结果:未计算")这句。

原文地址:https://www.cnblogs.com/liancs/p/3879365.html