简体   繁体   English

第一种方法抛出异常后如何恢复第二种方法 C#

[英]How to resume second method after first method throws an exception C#

While looking on C# try catch tutorial, I got following question.在查看 C# try catch 教程时,我得到了以下问题。 My sample code as follows,我的示例代码如下,

Inside mainMethod() , I need to call three separate methods.mainMethod()中,我需要调用三个单独的方法。 Inside testMethodOne() , I need to handle exception as.testMethodOne()内部,我需要将异常处理为。 If testMethodOne() throws exception, without executing testMethodTwo(dt) , mainMethod() throwing exception.如果testMethodOne()抛出异常,不执行testMethodTwo(dt)mainMethod()抛出异常。 I need to call testMethodTwo(dt);我需要调用testMethodTwo(dt); and testMethodThreee(dt);testMethodThreee(dt); if testMethodOne() throws exception, how can I do it.如果testMethodOne()抛出异常,我该怎么做。

public void MainMethod(data dt){

    try{
    
    testMethodOne(dt);
    testMethodTwo(dt);
    testMethodThreee(dt);   
    
    }catch(Exception ex){
    
    
        throw ex;
    
    }
}

public void testMethodOne(dt){
    try 
    {
      //  Block of code to try
    }
    catch (Exception e)
    {
      //  Block of code to handle errors
    }
} 

I understood your question as follows (but I might be wrong, your questions is not very clear):我对您的问题的理解如下(但我可能错了,您的问题不是很清楚):

Even if one of your testMethods throws an exception, you still want to continue in the normal program flow with the other methods.即使您的一个 testMethods 抛出异常,您仍然希望使用其他方法继续正常的程序流程。 If at least one of the method failed, mainMethod could then report this as AggregateException .如果至少有一个方法失败,则mainMethod可以将其报告为AggregateException

public void MainMethod(data dt)
{
    var exceptions = new List<Exception>();

    try
    {
        testMethodOne(dt);
    }
    catch (Exception ex)
    {
        exceptions.Add(ex);
    }

    try
    {
        testMethodTwo(dt);
    }
    catch (Exception ex)
    {
        exceptions.Add(ex);
    }

    try
    {
        testMethodThreee(dt);   
    }
    catch (Exception ex)
    {
        exceptions.Add(ex);
    }

    if (exceptions.Count > 0)
    {
        throw new AggregateException(exceptions);
    }
}

It seems as if you want exceptions to alter the flow of your main method without breaking everything.似乎您希望异常来改变您的主要方法的流程而不破坏一切。 One easy method is to make each 'testmethod' return a boolean.一种简单的方法是让每个“测试方法”返回 boolean。

public bool testMethodOne(dt){
    try 
    {
      //  Block of code to try
      return true;
    }
    catch (Exception e)
    {
      //  Block of code to handle errors
      return false;
    }
} 

Then in your main code you can go然后在你的主代码中你可以 go

if(!testMethodOne(dt))
    if(!testMethodTwo(dt))
        if(!testMethodThree(dt)) 
             //log that all methods failed

The above snippet would try each method until it finds one that succeeds.上面的代码片段会尝试每种方法,直到找到成功的方法。 If that's not the behaviour you are looking for can you reword your question to make it clearer?如果这不是您正在寻找的行为,您可以改写您的问题以使其更清楚吗? If you want the opposite to happen just get rid of the.如果您希望相反的情况发生,请摆脱。 and it will go until one fails, Alternatively you could put a throw in your catch statement in each of the testMethods.并且它将 go 直到失败,或者您可以在每个测试方法中的 catch 语句中添加一个 throw。 and that would stop execution once one is reached as well.一旦达到一个,那也会停止执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM