मैट के उत्तर के आधार पर देखें। यह वास्तव में कुल ओवरकिल है।
मैं ऐसा कुछ करने का सुझाव देता हूं, और यह सुनिश्चित करता हूं कि परीक्षण पास हो जाए। MSTest परीक्षण के लिए सबसे अच्छा पुस्तकालय नहीं है, लेकिन यह "मानक" है :)
[TestClass()]
public class FinalClauseTester
{
private TestContext testContextInstance;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestMethod]
[DeploymentItem(@"Something right goes here.")]
[DataSource("Something else goes here", "row", somethingOtherSetupCrap)]
public void TestFinalClause()
{
string[] allStrings = {"1", "2", "3", "4", "5"};
int yesCount = 0;
foreach(string s in allStrings)
{
try
{
//Error happens here
throw new Exception();
}
catch(Exception ex)
{
//Handle exception
if (yesCount == 3)
{
break;
}
}
finally
{
//Clean up code
yesCount++;
}
}
// And, at the end of this loop ...
Debug.Assert(yesCount = 3); // Or something like this.
}
}
स्रोत
2010-11-11 00:02:34
"क्या आपने इसे आजमाया है?" – Alex
मैंने एक प्रयोग चलाया, मैंने इस सवाल को बनाने में कितना समय लगेगा, और मैंने समय लगाया कि इसे कंसोल ऐप में रखने में कितना समय लगेगा और "रन" पर क्लिक करें। कंसोल जीता। ठीक है, मैंने वास्तव में एक प्रयोग नहीं किया था लेकिन आपको बिंदु मिल गया। :) – RPM1984
[* खांसी *] (http://stackoverflow.com/questions/4150186/breaking-out-of-a-for-loop-from-a-catch-block/4150190#4150190) –