【问题标题】:Getting "Creating default object from empty value" exception msg but getCode() returns 0获取“从空值创建默认对象”异常消息但 getCode() 返回 0
【发布时间】:2015-01-10 19:17:34
【问题描述】:

使用 Laravel 4.2.* 和 PHP 5.5.8 我有一个 try catch 块,可以正确捕获警告“从空值创建默认对象”。但是 getCode() 调用返回 0。

是否有任何方法可以设置我的环境,以便此异常和其他异常始终为 getCode() 返回非零值? (请注意,我已经捕获了 getCode 返回字符串的 PDOExceptions。)

try{
    //do something that causes the above exception;
catch($e){
    $code = $e->getCode();
}

帖子的重点不是导致错误的原因,而是 $e->getCode() 返回 0 值。我想使用 getCode() 没有保证值,因为它取决于引发异常的未知代码。

【问题讨论】:

  • 你能给你看代码吗

标签: php laravel-4


【解决方案1】:
(Note I'm already trapping for PDOExceptions where getCode returns a string.)

不知道你是如何从这个方法中得到string...

根据此处http://php.net/manual/en/exception.getcode.php 的文档,异常对象Gets the Exception code 上的getCode() 方法。

<?php
try {
    throw new Exception("Some error message", 30);
} catch(Exception $e) {
    echo "The exception code is: " . $e->getCode();
}
?>

将输出:

The exception code is: 30

因此,如果您从getCode() 方法中获取0,则很可能是在未明确设置异常代码的情况下引发的异常。例如throw new Exception("An error has occurred");

【讨论】:

  • 跟我想的差不多。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2013-03-05
  • 2020-05-28
  • 2020-04-09
  • 1970-01-01
  • 2015-01-28
相关资源
最近更新 更多