2014-11-25 39 views
-3

我有一個for循環包含一個try-catch塊:重新拋出的try-catch在for循環

for .. 
{ 
    try 
      { 
       ... 
      } 
    catch (Exception ex) 
      { 
       throw ex; 
      } 
} 

現在,如果我將重新拋出異常,並會抓住它的調用方法,將該程序繼續從下一次迭代? (在外部catch塊之後)。

TY!

+8

您可以自己嘗試嗎? – Tyress 2014-11-25 07:37:48

+0

請參閱[拋出異常最佳實踐](http://stackoverflow.com/questions/22623/throwing-exceptions-best-practices)。 – mihai 2014-11-25 07:37:58

回答

1

不,它不會。拋出你離開當前的方法(如果你沒有在這個方法中捕獲它),所以它就像一個回報。如果您在外部方法中捕獲異常,程序將繼續使用外部方法:

private void innerMethod() 
{ 
    try 
    { 
     throw; 
    } 
    catch 
    { 
     throw; 
    } 
    someMethodThatWillNotBeExecuted(); 
} 

public void outerMethod() 
{ 
    try 
    { 
     innerMethod(); 
    } 
    catch 
    { 
     thisWillBeExecuted(); 
    } 
    thisWillAlsoBeExecuted(); 
}