smltq 发表于 2008-6-17 20:03

这题我真晕了```有没人帮帮忙,新手,对异处理还不会

4.打开4文件夹中的项目,请为其添加异常处理模块,使得:
<1>当输入字符串格式不对时,在控制台上输出“请输入整数”;
<2>当j=0时,在控制台上输出“除数不能为0”;
<3>当输入的数字太大时,将该异常重新抛出;
<4>当有其他异常时,在控制台上输出“其他状况”。

以下是第四题的项目文件
using System;
using System.Collections.Generic;
using System.Text;

namespace _
{
    class Program
    { static void Main(string[] args)
        {
            int i = Convert.ToInt32(Console.ReadLine());
            int j = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(i / j);
}
    }
}

冰彩虹 发表于 2008-6-17 21:07

try
{
  int i = Convert.ToInt32(Console.ReadLine());
  int j = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine(i / j);
}
catch(InvalidCastException ex)
{
  Console.WriteLine("请输入整数");
}
catch(DivideByZeroException ex)
{
  Console.WriteLine("除数不能为0");
}
catch(OverflowException ex)
{
  threw ex;
}
catch(Exception ex)
{
  Console.WriteLine("其他状况");
}

smltq 发表于 2008-6-18 13:08

非常感谢

页: [1]

编程论坛