Can't catch Exception thrown by Carbon

断了今生、忘了曾经 提交于 2019-12-21 07:16:41

问题


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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/31248584/cant-catch-exception-thrown-by-carbon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!