【问题标题】:Can't catch Exception thrown by Carbon无法捕获 Carbon 抛出的异常
【发布时间】:2015-09-23 18:16:26
【问题描述】:

以下代码抛出了一个我无法捕获的异常。

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

还有其他人对 Carbon 有过这个问题吗?

【问题讨论】:

    标签: php laravel laravel-5 php-carbon


    【解决方案1】:

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

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

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

    【讨论】:

    • 您在这里回答问题时应该将您的答案标记为正确答案:)
    • 我会的,一旦 SO 提出问题的计时器消失。
    • 不用担心,您必须等待 48 小时才能接受自己的答案。 =/
    • 我想它可以阻止人们通过不断地提问和回答自己的问题来人为地提高分数。
    • 一个该死的反斜杠……
    猜你喜欢
    • 2019-01-14
    • 2021-09-02
    • 2011-04-05
    • 2013-08-02
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2016-10-27
    相关资源
    最近更新 更多