【问题标题】:C# If Method Failed (Catch), DoSomethingC# 如果方法失败(捕获),DoSomething
【发布时间】: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


【解决方案1】:

尽可能让异常冒泡。因此,从SometimesIFail 方法中删除try/catch,并在更靠近用户的地方捕获错误。像这样的:

try {
    SometimesIFail();
    // Do stuff 
} catch {
    // Tell the user an error has occurred.
}

请考虑例外情况 - 它们是例外情况,不应用于流量控制。如果您的代码有问题导致它有时崩溃,请改为修复问题。

【讨论】:

  • 这不是我想要的,看看编辑后的文字
  • 实际上,这正是您想要的,因为您不必告诉用户发生了错误,您可以什么都不做foreach 循环将只需继续列表中的下一项即可。但是吞下错误而不采取行动是一种非常糟糕的代码设计,您应该专注于解决问题
  • 不可能解决问题,因为我正在使用 Webclient 类并从网站获取 Url,有时 Url 失败(404 服务器错误);)
  • 您无法控制的服务器可能会失败,这不是意外错误。如果你得到一个 404,那是客户端请求 错误的 url 的错误!如果得到 500,则应该捕获 WebException,将错误存储在某处,最好告诉用户。继续执行列表中的下一项可能不是正确的方法,但这取决于您遇到的错误类型。
  • mhm 我按照你说的做了,但我没能解决我的问题,在他进入 catch 之后,他执行了进一步的代码,这不是想法.. 但无论如何我会标记这个作为回应,无论如何感谢您的承诺..:D
【解决方案2】:

如果你解决了你的问题,我认为没有 try catch 那么它会是更好的选择......

【讨论】:

  • 我不太明白你的意思?如果我删除 Try/Catch,程序将关闭 :)
【解决方案3】:

起初我以为我知道你在寻找什么,然后我阅读了代码 sn-p 中的 cmets,所以现在我不太确定。这是我根据我认为您想要的答案的答案。看起来您想检查SometimesIFail 方法是否成功,如果成功则执行一些代码,如果失败则继续下一次迭代。对于这种情况,我会采取以下措施:

// Don't use a void here, use a bool
public bool SometimesIFail(string text)
{
    try
    {
         //Anything
         return true;
    }
    catch(Exception)
    {
         //Anything
         return false;
    }
}

....

foreach (String text in texts)
{
    if(SometimesIFail(text)) // Evaluates to true for success
    {
        // Do your success matching code
    }

    // There doesn't need to be an else condition if you're
    // only passing to the next iteration
}

【讨论】:

  • 看看我编辑的答案,第一个问题解决了(在 try/catch 中使用 true/false)
  • @eMi:抱歉,在您编辑的答案中,第一个问题没有改变,只是其中有一个未使用的布尔值,其余部分和以往一样不清楚。
  • 是的,我必须返回真或假,但这不是我的主要问题......另一个问题很重要,但现在我将答案标记为已回复,我认为它可能很难理解..
【解决方案4】:

试试这个:

public bool SometimesIFail(string text) 
{ 
    try 
    { 
         //Anything
         return false;
    } 
    catch(Exception) 
    { 
         //Anything
         return true;
    } 
}

foreach (String text in texts)  
{  
    SometimesIFail(text);  
    if(SometimesIFail(text)) 
    { 
        // returned true - exception was thrown
        SometimesIFail(text);
    }   
    else  
    {  
        //Do Something  
    }  
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多