开发者

How do I conditionally throw an exception in a concise way?

开发者 https://www.devze.com 2023-01-29 21:04 出处:网络
I need to throw an exception if a statement returns false and am trying to come up with a simple, concise way to do it. Any ideas? I wanted to do the following...

I need to throw an exception if a statement returns false and am trying to come up with a simple, concise way to do it. Any ideas? I wanted to do the following...

<?
  // ...
  oci_execute( $stmt ) or throw new OracleException( $stmt );
  // ...
?>

... but the PHP interpreter won't let me do it (syntax error) because 'throw' is a language construct.

Any ideas? I would like a very short one liner (no 开发者_开发技巧if statements please)


<?
  // ...
  if (!oci_execute( $stmt )) throw new OracleException( $stmt );
  // ...
?>

is everything that comes my mind. But I've used PHP for not too long. Now I've discovered Perl which is a great thing and you can use the construct you suggest and many more. Always worth a try!


You can't. However, I have never actually had that particular need that you're describing, which leads me to suspect that you are doing something a bit strange. Or at least un-idiomatic. The presence of a procedural function (oci_execute) seems to support that.

I don't have any experience with Oracle from php, but could you use the pdo-bindings instead? That will give you a more object oriented interface to work with.


Would

if !oci_execute($stmt) { throw new OracleException($stmt); }

work?

0

精彩评论

暂无评论...
验证码 换一张
取 消