日期:2014-05-18  浏览次数:20932 次

try-catch-finally语句不会用啊~~~求指教~~~
static void Main(string[] args)
  {
  double x, y;
  try
  {  
  x = double.Parse(Console.ReadLine());
  y = double.Parse(Console.ReadLine());
  }
  catch(System.FormatException)
  {
  Console.WriteLine("出错啦!/t请输入数字!");
  }
   
  Console.WriteLine("{0}+{1}={2}", x, y, x + y);
  Console.WriteLine("{0}/{1}={2}", x, y, x / y);

------解决方案--------------------
static void Main(string[] args)
{
double x, y;
try
{
x = double.Parse(Console.ReadLine());
y = double.Parse(Console.ReadLine());
Console.WriteLine("{0}+{1}={2}", x, y, x + y);
if(y != 0)
Console.WriteLine("{0}/{1}={2}", x, y, x / y);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}