【问题标题】:Why is it that throw new exception does not echo the message?为什么抛出新异常不回显消息?
【发布时间】:2013-05-24 01:49:10
【问题描述】:

我一直在尝试在我的 php 函数中抛出新的异常,但它没有回显错误消息。我本可以改用常规回声,但我真的很想看看有什么不同。请告诉我这段代码有什么问题。

function searchMovie($movieName){
      if (!isset($movieName))
        throw new Exception('missing movie name');
      //execute
      //return something here
}
 //call the function
 echo searchMovie('iron man'); //this gives a result.

 echo searchMovie(); //this does not echo the 'missing movie name'

谢谢。对于像我这样的菜鸟的任何帮助将不胜感激。

【问题讨论】:

    标签: php error-handling throw


    【解决方案1】:

    应该是这样的

    function searchMovie($movieName){
      if (!isset($movieName))
      { 
        throw new Exception('missing movie name');
      //execute
      //return something here
      }
    }
    

    【讨论】:

      【解决方案2】:

      抛出异常只是过程的一部分。您还必须catch 它并尝试以有意义的方式恢复或向用户显示错误

      【讨论】:

        猜你喜欢
        • 2013-11-13
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 2014-07-09
        • 2017-07-03
        相关资源
        最近更新 更多