dongshang6062 2009-07-31 08:04
浏览 54
已采纳

PHP:'或'语句失败:如何抛出新的异常?

Everyone here should know the 'or' statemens, usually glued to an die() command:

$foo = bar() or die('Error: bar function return false.');

The most of the times we see something like:

mysql_query('SELECT ...') or die('Error in during the query');

However, i cant understand how exactly that 'or' statement works.

I would like to throw a new exception instead of die(), but:

try{
    $foo = bar() or throw new Exception('We have a problem here');

Doesnt work, and neither

$foo = bar() or function(){ throw new Exception('We have a problem here'); }

The only way i found to do that is this horrible thought:

function ThrowMe($mess, $code){
    throw new Exception($mess, $code);
}
try{
    $foo = bar() or ThrowMe('We have a problem in here', 666);
}catch(Exception $e){
    echo $e->getMessage();
}

But there is a way to throw a new exception directly after the 'or' statement?

Or this kind of structure is mandatory (i dont liek the ThrowMe function at all):

try{
    $foo = bar();
    if(!$foo){
        throw new Exception('We have a problem in here');
    }
}catch(Exception $e){
    echo $e->getMessage();
}

Edit: what i want is really to avoid the use of an if() check every potential dangerous operation i do, for example:

#The echo $e->getMessage(); is just an example, in real life this have no sense!
try{
    $foo = bar();
    if(!$foo){
        throw new Exception('Problems with bar()');
    }
    $aa = bb($foo);
    if(!$aa){
        throw new Exception('Problems with bb()');
    }
    //...and so on!
}catch(Exception $e){
    echo $e->getMessage();
}

#But i relly prefer to use something like:

try{
    $foo = bar() or throw new Exception('Problems with bar()');
    $aa = bb($foo) or throw new Exception('Problems with bb()');
    //...and so on!
}catch(Exception $e){
    echo $e->getMessage();
}

#Actually, the only way i figured out is:

try{
    $foo = bar() or throw new ThrowMe('Problems with bar()', 1);
    $aa = bb($foo) or throw new ThrowMe('Problems with bb()', 2);
    //...and so on!
}catch(Exception $e){
    echo $e->getMessage();
}

#But i'll love to thro the exception directly instead of trick it with ThrowMe function.
  • 写回答

7条回答 默认 最新

  • doujiang3997 2009-07-31 09:16
    关注

    or is just a logical operator, and it's analogous to ||.

    The common trick of

    mysql_query() or die();
    

    could just as well be written

    mysql_query() || die();
    

    What happens here is the "logical or" operator (whichever you choose) is trying to determine if either operand evaluates to TRUE. This means the operands must be expressions that can be cast as a boolean.

    So, the reason

    bar() or throw new Exception();
    

    is illegal, is because

    (boolean)throw new Exception();
    

    is also illegal. In essence, the process of throwing an exception doesn't generate a return value for the operator to check.

    But calling a function does generate a return value for the operator to check (no explicit return value will result int the function returning NULL which casts as FALSE) which is why it works for you when you wrap exception throwing in a function.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100