16

这在PHP中可能吗?

try {

  $obj = new Clas();

  if ($obj->foo) {
    // how to exit from this try block?
  }

  // do other stuff here

} catch(Exception $e) {

}

我知道我可以将其他内容放在 之间{},但这会增加对更大代码块的缩进,我不喜欢它:P

4

8 回答 8

22

当然是goto

try {

  $obj = new Clas();

  if ($obj->foo) {
    goto break_free_of_try;
  }

  // do other stuff here

} catch(Exception $e) {

}
break_free_of_try:
于 2014-01-18T00:55:18.290 回答
14

好吧,没有理由这样做,但是你可以在你的try块中强制一个异常,停止你的函数的执行。

try {
   if ($you_dont_like_something){
     throw new Exception();
     //No code will be executed after the exception has been thrown.
   }
} catch (Exception $e){
    echo "Something went wrong";
}
于 2013-05-09T19:38:38.843 回答
7

我也遇到过这种情况,并且像您一样,不想要无数 if/else if/else if/else 语句,因为它使代码的可读性降低。

我最终用我自己的扩展了 Exception 类。下面的示例类用于验证问题,触发时会产生不太严重的“日志通知”

class ValidationEx extends Exception
{
    public function __construct($message, $code = 0, Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    public function __toString()
    {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }
}

在我的主要代码中,我称之为;

throw new ValidationEx('You maniac!');

然后在 Try 语句的末尾我有

        catch(ValidationEx $e) { echo $e->getMessage(); }
        catch(Exception $e){ echo $e->getMessage(); }

欢迎评论和批评,我们都在这里学习!

于 2015-03-11T22:09:01.920 回答
4

你就不能这样吗?

try{

  $obj = new Clas();

  if(!$obj->foo){
  // do other stuff here
  }


}catch(Exception $e){

}
于 2013-05-09T19:34:55.670 回答
4
try
{
    $object = new Something();
    if ($object->value)
    {
        // do stuff
    }
    else
    {
        // do other stuff
    }
}
catch (Exception $e)
{
     // handle exceptions
}
于 2013-05-09T19:48:32.003 回答
1

在 php 5.3+ 中使用带有 try catch 块的异常的好处是,您可以创建自己的异常并在需要的时间和方式下处理它们。请参阅:扩展异常

class AcceptedException extends \Exception 
{
    //...
}

然后,您可以捕获特定异常或if ($e instanceof AcceptedException)在您的catch \Exception块中使用以确定您希望如何处理异常。

  1. 处理示例:http: //ideone.com/ggz8fu
  2. 未处理示例:http: //ideone.com/luPQel
try {
    $obj = (object) array('foo' => 'bar');
    if ($obj->foo) {
        throw new \AcceptedException;
    }
} catch (\AcceptedException $e) {
    var_dump('I was accepted');
} catch (\Exception $e) {
    if ($e instanceof \InvalidArgumentException) {
        throw $e; //don't handle the exception
    }
}

与许多替代解决方案相比,这使您的代码更具可读性并且更容易排除故障。

于 2016-09-16T16:55:06.373 回答
0

我个人喜欢通过使用退出 try/catch 语句

throw new MyException("optional message", MyException::ERROR_SUCCESS);

我显然通过使用:

switch($e->getCode()) {
   /** other cases make sense here */
   case MyException::ERROR_SQL:
       logThis("A SQL error occurred. Details: " . $e->getMessage());
   break;

   case MyException::ERROR_SUCCESS:
       logThis("Completed with success. Details: " . $e->getMessage());
   break;

   case MyException::ERROR_UNDEFINED:
   default:
       logThis("Undefined error. Details: " . $e->getMessage());
   break;
}
于 2019-10-16T06:19:58.117 回答
0

这就是我这样做的方式:

<?php
echo 'this' . PHP_EOL;
switch(true) {
    default:
        try {
            echo 'is' . PHP_EOL;
            break;
            echo 'not' . PHP_EOL;
        } catch (Exception $e) {
            // error_log($e->getMessage());
        }
}
echo 'fun !';

:-)

于 2021-08-06T12:36:10.843 回答