首页 > 学院 > 开发设计 > 正文

C#中关于try...catch...finally的一些技巧

2019-11-17 04:06:11
字体:
来源:转载
供稿:网友
view plaincopy to clipboardPRint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
static string[] _arrStr_eTypes = {"index", "nested index" };   
  
static void Main(string[] args)   
{   
  foreach(string str_eType in _arrStr_eTypes)   
  {   
    try  
    {   
      ThrowException(str_eType);   
    }   
    catch(IndexOutOfRangeException e)   
    {   
       Console.WriteLine("Main() System.IndexOutOfRangeException catch" +   
            " block reached. Message:/n/"{0}/"", e.Message);   
    }   
    finally  
    {   
       Console.WriteLine();   
    }   
       
    Console.ReadKey();       
  }   
  static void ThrowException(string str_exceptionType)   
  {   
    switch(str_exceptionType)   
    {   
      case "index":   
      {   
        _arrStr_eTypes[2] = "error";   
        break;   
      }   
      case "nested index":   
      {   
         try  
         {   
           ThrowException("index");      
         }   
         catch(IndexOutOfRangeException e)   
         {   
           Console.WriteLine("ThrowException System.IndexOutOfRangeException catch" +   
              " block reached. Message:/n/"{0}/"", e.Message);   
           //throw;   
         }   
         finally  
         {   
         }   
         break;   
      }   
    }   
  }   
}  
static string[] _arrStr_eTypes = {"index", "nested index" };

static void Main(string[] args)
{
  foreach(string str_eType in _arrStr_eTypes)
  {
    try
    {
      ThrowException(str_eType);
    }
    catch(IndexOutOfRangeException e)
    {
       Console.WriteLine("Main() System.IndexOutOfRangeException catch" +
            " block reached. Message:/n/"{0}/"", e.Message);
    }
    finally
    {
       Console.WriteLine();
    }
    
    Console.ReadKey();    
  }
  static void ThrowException(string str_exceptionType)
  {
    switch(str_exceptionType)
    {
      case "index":
      {
        _arrStr_eTypes[2] = "error";
        break;
      }
      case "nested index":
      {
         try
         {
           ThrowException("index");   
         }
         catch(IndexOutOfRangeException e)
         {
           Console.WriteLine("ThrowException System.IndexOutOfRangeException catch" +
              " block reached. Message:/n/"{0}/"", e.Message);
           //throw;
         }
         finally
         {
         }
         break;
      }
    }
  }
}

上述代码运行后得出,当str_eType 分别为 "index"和"nested index"的时候,都是数组越界错误,但是显示的信息却是不同的。

当str_eType为"index"的时候,它显示的是Main函数中catch的内容;当str_eType为"nested index"的时候,它显示的是ThrowException函数中catch的内容,而Main函数中catch的内容就不显示了。

由此可见,当一个函数被另一个函数调用的时候,只要调用的函数在try范围内。那如果被调用函数出现错误,错误将被调用函数获得,例:当str_eType为"index"的时候;当函数出现嵌套调用的时候,出错的函数将被它最近的包含try的调用函数进行处理,例如:当str_eType为"nested index"的时候,虽然Main函数和ThrowException都有关于数组越界的处理,但是由于ThrowException是最近的调用,所以它就调用ThrowException中的错误处理。而当注释的"throw"作为代码的时候,除了执行ThrowException中的catch部分和finally部分之后还会执行上一级关于错误的处理。

换个理解方法,函数存在的目的是为了优化代码结构,即在函数调用的地方可以直接将函数中的代码贴进去

将ThrowException的位置替换为ThrowException中的代码,效果是一样的。既然如此,ThrowException的位置包含在try范围内

当str_eType为"index"的时候,出现了数组越界错误,ThrowException中又没有运行到错误处理的方法,代码相当于

try //Main中的

{

   //错误
}

catch

{
}

而当str_eType为"nested index"的时候代码相当于

try //Main中的

{

  try //ThrowException中的

  {

    //错误
  }

  catch

  {
  }

}

catch

{
}

对ThrowException中的try来说,是有错误,而相对于Main中的try来说,并没有错误(错误已经解决掉了),自然就不会触发catch
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表