trycatch语句:C#语言try-catch异常处理语句

  try-catch处理表达式允许将任何可能发生异常情形代码放置在try{}代码块进行监控真正处理异常代码则被放置在catch{}块里面个try{}块可对应多个catch{}块     举例 try-catch语句写入多个catch使用

  通过两个catch语句进行捕获异常它们分别是ArgumentNullException异常和Exception异常代码如下

using;
MainClass
{
  voidProcessString(str)
  {
    (strnull)
    {
      throwArgumentNullException;
    }
  }
  voidMain
  { 
    Console.WriteLine("输出结果为:");
    try
    {
      str=null;
      ProcessString(str);
    }
    catch(ArgumentNullExceptione)
    {
      Console.WriteLine("{0}Firstexception.",e.Message);
    }
    catch(Exceptione)
    {
      Console.WriteLine("{0}Secondexception.",e.Message);
    }
  }
}


Tags:  trycatchfinally javatrycatch trycatch trycatch语句

延伸阅读

最新评论

发表评论