《C#入门详解》刘铁锰 表达式,语句详解

时间:2020-09-24 20:54:13   收藏:0   阅读:61

技术图片

 

class program
{
 static void main(string[] args)
 {
  calculator c = new calculator();
  int r           = 0;
  try
  {
   r               = c.add("abc","100");
   }
   catch(overflowException oe)
   {
   console.writeline(oe.message);
    }
  }
}

class Calculator
{
  public int Add(string arg1 , string arg2)
  {
   int a = 0;
   int b = 0;
   try
   {
    a = int.parse(arg1);
    b = int.parse(arg2);
    }
    catch(ArgumentNullException)
    {console.writeling("your arguments are null");}
    catch(FormatException)
    {console.writeling("your arguments are not number");}    
    catch(OverFlowException oe)
    {
      //console.writeline("out of range");
     throw oe;//只希望处理上面的两个异常,把overflow这个异常抛出去,意思是谁调用          这个add方法,谁去抓住这个异常进行处理
     }
/*可以在catch后面的圆括号内加上标识符( catch(ArgumentNullException ane)),可以打印出具体错误消息。*/
/*try catch finally,当执行try语句的时候,无论是否发生异常,finally语句永远会执行,finally语句中一般会写两类内容,第一类数据库链接总能关闭,第二类写程序的log*/
  }
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!