无法捕获Carbon引发的异常 [英] Can't catch Exception thrown by Carbon

查看:237
本文介绍了无法捕获Carbon引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code throws an exception that I can't catch for whatever reason.

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

现在,如果我将其放在函数闭包中的路由文件中,则可以正常工作.从模型中调用它只会引发异常.

Now, if I put this in my routes file in a function closure, it works fine. It's only throwing an exception when it's called from the Model.

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

还有其他人遇到过碳的问题吗?

Has anyone else had this issue with Carbon?

推荐答案

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

Immediately after posing the question, I thought of the answer. Since I saw a lot of posts online about a very similar issue, I figured I'd go ahead and answer this one.

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

The issue was namespacing. Carbon was running in a different namespace from my app (obviously) so when I tried to catch (Exception) I was actually trying to catch an exception within my app's namespace. Changing the catch statement to catch (\Exception) resolved it.

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

I hope this helps anyone that ends up on this page.

这篇关于无法捕获Carbon引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆