19

以下代码引发了一个我无法捕获的异常,无论出于何种原因。

try {
    $this->date = \Carbon\Carbon::parse($this->date)->toDateString();
}
catch (Exception $err) {
    $this->date = \Carbon\Carbon::parse("January 1st 1900")->toDateString();
}

现在,如果我将它放在函数闭包中的路由文件中,它可以正常工作。它仅在从模型中调用时抛出异常。

exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (Summer 2015) at position 0 (S): The timezone could not be found in the database' in /home/vagrant/www/steamcompare/vendor/nesbot/carbon/src/Carbon/Carbon.php:222

有没有其他人遇到过碳这个问题?

4

1 回答 1

56

提出问题后,我立即想到了答案。由于我在网上看到很多关于一个非常相似的问题的帖子,我想我会继续回答这个问题。

问题是命名空间。Carbon 在与我的应用程序不同的命名空间中运行(显然),所以当我尝试时,catch (Exception)我实际上是在尝试在我的应用程序的命名空间中捕获异常。更改 catch 语句以catch (\Exception)解决它。

我希望这对最终访问此页面的任何人有所帮助。

于 2015-07-06T14:39:19.417 回答