PHP:'或'指令语句失败:如何抛出一个新的异常? [英] PHP: 'or' statement on instruction fail: how to throw a new exception?

查看:194
本文介绍了PHP:'或'指令语句失败:如何抛出一个新的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都应该知道'或'状态,通常粘贴到一个die()命令:

  $ foo = bar ()或die('Error:bar function return false。'); 

大多数时候,我们看到如下:

  mysql_query('SELECT ...')或死(查询期间出错); 

但是,我无法理解或语句的工作原理。



我想抛出一个新的异常而不是die(),但是:

  try { 
$ foo = bar()或抛出新的异常('我们在这里有问题');

不工作,既不

  $ foo = bar()或function(){throw new Exception('这里有一个问题) 

我发现这样做的唯一方法是这个可怕的想法:

 函数ThrowMe($ mess,$ code){
抛出新的异常($ mess,$ code);
}
尝试{
$ foo = bar()或ThrowMe('我们在这里有一个问题',666);
} catch(异常$ e){
echo $ e-> getMessage();
}

但是有一种方法可以直接在或声明?



或者这种结构是强制性的(我根本不会抛弃ThrowMe功能):

  try {
$ foo = bar();
if(!$ foo){
throw new Exception('这里有一个问题)
}
} catch(异常$ e){
echo $ e-> getMessage();
}

编辑:我想要的是真的要避免使用if()来检查每个潜在的危险操作,例如:

 #echo $ e->的getMessage();只是一个例子,在现实生活中没有任何意义! 
try {
$ foo = bar();
if(!$ foo){
throw new Exception('problems with bar()');
}
$ aa = bb($ foo);
if(!$ aa){
throw new Exception('problems with bb()');
}
//...等等!
} catch(异常$ e){
echo $ e-> getMessage();
}

#But我宁愿喜欢使用以下东西:

尝试{
$ foo = bar()或抛出新的异常('问题与bar()');
$ aa = bb($ foo)或抛出新的异常('bb()的问题');
//...等等!
} catch(异常$ e){
echo $ e-> getMessage();
}

#实际上,我想出的唯一方法是:

尝试{
$ foo = bar()或抛出新的ThrowMe(' bar()'的问题,1);
$ aa = bb($ foo)或抛出新的ThrowMe('问题与bb()',2);
//...等等!
} catch(异常$ e){
echo $ e-> getMessage();
}

#But我会喜欢直接阻止异常,而不是用ThrowMe功能来欺骗它。


解决方案

只是逻辑运算符,它类似于



<$ p



$ p> mysql_query()或die();

也可以写成

  mysql_query()||死(); 

这里发生的是逻辑或运算符(无论选择哪个)正在尝试确定是否操作数的计算结果为TRUE。这意味着操作数必须是可以转换为布尔值的表达式。



所以,原因

  bar()或抛出新的Exception(); 

是非法的,是因为

 (boolean)throw new Exception(); 

也是非法的。实质上,抛出异常的过程不会为运算符生成一个返回值来检查。



但是调用函数不会生成操作符检查的返回值(不显式返回值将导致返回 NULL 的函数转换为 FALSE )这就是为什么当你把异常抛在一个函数中的时候它为你工作。



希望有帮助。


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.

解决方案

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.

这篇关于PHP:'或'指令语句失败:如何抛出一个新的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆