【问题标题】:php try ... elsephp尝试...否则
【发布时间】:2011-03-22 20:10:43
【问题描述】:

PHP 中是否有类似于 Python 中的 try ... else 的东西?

我需要知道try块是否正确执行,当块正确执行时,会打印一条消息。

【问题讨论】:

    标签: php exception


    【解决方案1】:

    PHP 没有 try/catch/else。但是,您可以在 catch 块中设置一个变量,该变量可用于确定它是否已运行:

    $caught = false;
    
    try {
        // something
    } catch (Exception $e) {
        $caught = true;
    }
    
    if (!$caught) {
    
    }
    

    【讨论】:

    • 如果有多个 catch 块,这将充满 $caught = true; 语句。
    • 一个小提示,python 的 else 块只有在 $caught 为假而不是真时才会被执行...
    【解决方案2】:

    我认为“else”子句有点限制,除非你不关心那里抛出的任何异常(或者你想冒泡这些异常)......根据我对 Python 的理解,它基本上相当于这个:

    try {
        //...Do Some Stuff Here
        try {
            // Else block code here
        } catch (Exception $e) {
            $e->elseBlock = true;
            throw $e;
        }
    } catch (Exception $e) {
        if (isset($e->elseBlock) && $e->elseBlock) {
            throw $e;
        }
        // catch block code here
    }
    

    所以它有点冗长(因为你需要重新抛出异常),但它也会像 else 子句一样在堆栈中冒泡......

    编辑或者,更简洁的版本(仅限 5.3)

    class ElseException extends Exception();
    
    try {
        //...Do Some Stuff Here
        try {
            // Else block code here
        } catch (Exception $e) {
            throw new ElseException('Else Clasuse Exception', 0, $e);
        }
    } catch (ElseException $e) {
        throw $e->getPrevious();
    } catch (Exception $e) {
        // catch block code here
    }
    

    编辑 2

    重新阅读您的问题,我认为您可能使用“else”块使事情过于复杂...如果您只是打印(不太可能引发异常),那么您实际上并不需要其他块:

    try {
        // Do Some stuff
        print "Success";
    } catch (Exception $e) {
        //Handle error here
        print "Error";
    }
    

    该代码只会打印 either SuccessError... 永远不会同时打印两者(因为如果 print 函数抛出异常,它实际上不会被打印。 . 但我不认为print 可以抛出异常...)。

    【讨论】:

    • 在我的情况下,将下一个 try{} 放入 catch{} 内效果很好
    • @ircmaxell 您的“编辑 2”答案是一个很好的答案。也许将其作为单独的答案发布,以便它可以自行投票。
    【解决方案3】:

    不熟悉 python,但听起来你在使用 Try Catch 异常块......

    http://php.net/manual/en/language.exceptions.php

    【讨论】:

      【解决方案4】:

      php中有try-catch

      示例:

      function inverse($x) {
          if (!$x) {
              throw new Exception('Division by zero.');
          }
          else return 1/$x;
      }
      
      try {
          echo inverse(5) . "\n";
          echo inverse(0) . "\n";
      } catch (Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
      
      // Continue execution
      echo 'Hello World';
      

      【讨论】:

      • 问题是关于 php 相当于 python 的“try-else”,即在没有抛出异常的情况下,在 try 块之后执行代码块的紧凑方式。
      【解决方案5】:
      try {
          $clean = false;
          ...
          $clean = true;
      } catch (...) { ... }
      
      if (!$clean) {
          //...
      }
      

      这是你能做的最好的。

      【讨论】:

      • 您应该在进入 try..catch 块之前将 $clean 设置为其初始值。请参阅 webbiedave 的回答。
      • @jmz 嗯?为什么?它无动于衷; PHP 中没有块作用域。
      【解决方案6】:

      您可以使用try { } catch () { }throw。见http://php.net/manual/en/language.exceptions.php

      try {
          $a = 13/0; // should throw exception
      } catch (Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
      

      或手动:

      try {
          throw new Exception("I don't want to be tried!");
      } catch (Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
      

      【讨论】:

      • 我没有投反对票,但他问的是else 块,一般不是例外......
      • 哦,$a = 13/0; 不会抛出异常,除非您安装了抛出异常的错误处理程序 (set_error_handler(function($errno, $errstr, $errfile, $errline) { throw new ErrorException($errstr, 0, $errno, $errfile, $errline);});...php.net/manual/en/class.errorexception.php)
      • PHP 的“catch”块不等同于 Python 的“else”块吗?
      • @enobrev:不,不是。请参阅下面的答案。
      • @enobrev,区别在于else 子句中抛出的异常不会被 except/catch 块捕获。因此,除了将 else 放在 try 中(因为我的答案试图传达)之外,还需要更多的逻辑......
      猜你喜欢
      • 1970-01-01
      • 2015-08-19
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多