国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#中關(guān)于try...catch...finally的一些技巧

2019-11-17 04:06:11
字體:
供稿:網(wǎng)友
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;
      }
    }
  }
}

上述代碼運(yùn)行后得出,當(dāng)str_eType 分別為 "index"和"nested index"的時(shí)候,都是數(shù)組越界錯(cuò)誤,但是顯示的信息卻是不同的。

當(dāng)str_eType為"index"的時(shí)候,它顯示的是Main函數(shù)中catch的內(nèi)容;當(dāng)str_eType為"nested index"的時(shí)候,它顯示的是ThrowException函數(shù)中catch的內(nèi)容,而Main函數(shù)中catch的內(nèi)容就不顯示了。

由此可見,當(dāng)一個(gè)函數(shù)被另一個(gè)函數(shù)調(diào)用的時(shí)候,只要調(diào)用的函數(shù)在try范圍內(nèi)。那如果被調(diào)用函數(shù)出現(xiàn)錯(cuò)誤,錯(cuò)誤將被調(diào)用函數(shù)獲得,例:當(dāng)str_eType為"index"的時(shí)候;當(dāng)函數(shù)出現(xiàn)嵌套調(diào)用的時(shí)候,出錯(cuò)的函數(shù)將被它最近的包含try的調(diào)用函數(shù)進(jìn)行處理,例如:當(dāng)str_eType為"nested index"的時(shí)候,雖然Main函數(shù)和ThrowException都有關(guān)于數(shù)組越界的處理,但是由于ThrowException是最近的調(diào)用,所以它就調(diào)用ThrowException中的錯(cuò)誤處理。而當(dāng)注釋的"throw"作為代碼的時(shí)候,除了執(zhí)行ThrowException中的catch部分和finally部分之后還會(huì)執(zhí)行上一級(jí)關(guān)于錯(cuò)誤的處理。

換個(gè)理解方法,函數(shù)存在的目的是為了優(yōu)化代碼結(jié)構(gòu),即在函數(shù)調(diào)用的地方可以直接將函數(shù)中的代碼貼進(jìn)去

將ThrowException的位置替換為ThrowException中的代碼,效果是一樣的。既然如此,ThrowException的位置包含在try范圍內(nèi)

當(dāng)str_eType為"index"的時(shí)候,出現(xiàn)了數(shù)組越界錯(cuò)誤,ThrowException中又沒有運(yùn)行到錯(cuò)誤處理的方法,代碼相當(dāng)于

try //Main中的

{

   //錯(cuò)誤
}

catch

{
}

而當(dāng)str_eType為"nested index"的時(shí)候代碼相當(dāng)于

try //Main中的

{

  try //ThrowException中的

  {

    //錯(cuò)誤
  }

  catch

  {
  }

}

catch

{
}

對(duì)ThrowException中的try來說,是有錯(cuò)誤,而相對(duì)于Main中的try來說,并沒有錯(cuò)誤(錯(cuò)誤已經(jīng)解決掉了),自然就不會(huì)觸發(fā)catch
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 紫阳县| 会昌县| 山阴县| 临高县| 泾川县| 缙云县| 贵定县| 东兰县| 呼和浩特市| 南丹县| 安塞县| 左云县| 宁化县| 牙克石市| 泌阳县| 德兴市| 镇平县| 淄博市| 盐城市| 郎溪县| 阿拉尔市| 金秀| 开阳县| 依安县| 五华县| 泾川县| 金阳县| 南投市| 哈巴河县| 柳林县| 灌云县| 两当县| 横山县| 酒泉市| 金坛市| 禄丰县| 郯城县| 商洛市| 观塘区| 昌黎县| 丰城市|