【问题标题】:Is it possible to conditionally execute a statement if there is no exception?如果没有异常,是否可以有条件地执行语句?
【发布时间】:2019-02-16 16:48:57
【问题描述】:

考虑以下函数。我是用 C# 写的。

public void Test()
{
    statement1;
    try
    {
        statement2;
    }
    catch (Exception)
    {
        //Exception caught
    }
}

只有当statement2 没有引起异常时,我才想执行statement1。仅当statement2 不引发任何异常时,是否可以执行statement1

【问题讨论】:

  • 通过将statement1;移动到statement2;下?
  • 你想要一台时光机?
  • 是什么阻止你这样做?从您在问题中描述的问题来看,S.Akbari 的回答是正确的。
  • 您可以设置计时器或等待。但是,这不是好方法。
  • @Prasanth:别担心。如果在确定添加这些字段是否成功之前需要使用 PDF,则只需临时制作 PDF。您可能甚至不会注意到这些毫秒。

标签: c# .net exception try-catch


【解决方案1】:

更改语句的顺序和逻辑。您无法预见运行时的异常

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,这就是您想要的(将statement1; 移动到statement2; 下):

    try
    {
        statement2;
        statement1;
    }
    catch (Exception)
    {
        //Exception caught
    }
    

    通过这种方式,statement1只有在statement2没有引起异常的情况下才会执行!

    【讨论】:

      【解决方案3】:

      是的,并且您实际上正在使用它(但错误)。

      try...catch 块用于捕获异常并采取适当的措施,无论是否抛出异常:

      try
      {
          // risky operation that might throw exception
          RiskyOperation();
          // here code is executed when no exception is thrown
      }
      catch(Exception ex)
      {
          // code here gets executed when exception is thrown
      }
      finally
      {
          // this code evaluates independently of exception occuring or not
      }
      

      总结一下,你需要做:

      try
      {
          statement2;
          statement1;
      }
      catch(Exception ex)
      {
          // code here gets executed when exception is thrown
      }
      

      【讨论】:

        【解决方案4】:

        是的,您可以通过这种方式轻松做到这一点

        public void Test()
        {
            try
            {
                statement2;
                statement1;
            }
            catch (Exception)
            {
                //Exception caught
            }
        }
        

        如果 statement2 抛出一些异常,statement1 将不会运行。

        另一种不太酷的方法是使用变量

        public void Test()
            {
                bool completed=false;
                try
                {
                    statement2;
                    completed=true;
                }
                catch (Exception)
                {
                    //Exception caught
                }
                if (completed)
                  statement1;
            }
        

        【讨论】:

          【解决方案5】:

          您可以选择递归,但我们需要确保它不会以无限循环结束。

          public void Test()
          {
             bool hasException = false;
             statement1;
             try
             {
                 statement2;
             }
             catch (Exception)
             {
                 hasException = true;
             }
             finally
             {
                 if(hasException)
                     Test();
             }
          }
          

          【讨论】:

          • 等等什么?是什么让您相信如果发生异常重新运行相同的代码会在某个时候起作用?
          • :) 好吧,根据功能上下文,程序员有责任确保程序必须在某个阈值点退出。
          • 是的,但实际上,您的代码很可能在第一次运行时运行良好,或者最终陷入无限循环。我的意思是如果statement2 第一次抛出异常,它很可能会在第二次以及之后的每次抛出异常。 (“精神错乱:一遍又一遍地做同样的事情,却期待不同的结果。”)
          • 有很多方法可以退出,例如你可以有一个全局变量 var RetryCount = 5; , 在每次递归调用时增加这个 var 并在达到 6 时退出。这完全取决于需求和上下文。
          【解决方案6】:

          异常后可以调用方法,

          public void Test(bool noerror= false)
          {
             if (noerror)
                statement1;
          
              try
              {
                  statement2;
                  completed=true;
              }
              catch (Exception)
              {
                   noerror=true;
                  Test(noerror)
                  //Exception caught
              }
          
          }
          

          【讨论】:

          • 等等什么?是什么让你相信如果发生异常重新运行相同的代码会在某个时候起作用?还有completed来自哪里?
          【解决方案7】:

          是的,您可以,您只需将语句 1 移到语句 2 下,因为只有在语句 2 没有引发任何异常时编译器才会到达语句 1。代码如下:

           public void Test()
          {
          try
          {
              statement2;
              statement1;
          }
          catch (Exception)
          {
              //Exception caught
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-01-24
            • 1970-01-01
            • 1970-01-01
            • 2020-10-27
            • 1970-01-01
            • 2011-10-18
            • 1970-01-01
            相关资源
            最近更新 更多