【发布时间】:2011-12-06 11:28:38
【问题描述】:
想象一下,你有一个方法:
public void SometimesIFail(string text)
{
bool everythingOk = true;
try
{
//Anything
}
catch(Exception)
{
//Anything
everythingOk = false
}
}
现在我想做这样的事情:
foreach (String text in texts)
{
if(!SometimesIFail(text)) //If SometimesIFail() Failed (walked into Catch) Do the same for the next TEXT from the List: texts
{
SometimesIFail(text); // The Next Text - Until iterated through all the texts..
//FROM HERE ON, I HAVE A RECURSIVE CALL, THAT MEANS THAT THIS CODE, MUSTNT BE EXECUTED
//Any Code..
}
else
{
//Do Something
}
}
解决问题的最佳方法是什么?
编辑:
测试后(检查是否正常),我想做一些事情,当它不正常时:
foreach (String text in texts)
{
if(!SometimesIFail(text))
{
//HERE I will do SometimesIFail(text) for the next text (in foreach)
// And here is a Recursive Call which should be called, after the foreach iterated through all the texts..
}
}
【问题讨论】:
-
让我们从将 'void' 更改为 'bool' 开始,因为您正在检查 'if(SometimesIFail(text))',有时 IFail 必须返回 True\False。
-
很难确定您的问题到底是什么或您要做什么......
-
你的意思是我应该设置一个本地布尔变量,并设置它的标志 true/false,每当它进入 try 或 catch 时?
-
您无法从捕获所有内容的
void方法中获得太多信息。也许在这里详细说明你真正需要的东西。 -
你还没有发布任何递归代码。有时 IFail 不会调用自己。
标签: c# if-statement try-catch