• <i id='DPz3t'><tr id='DPz3t'><dt id='DPz3t'><q id='DPz3t'><span id='DPz3t'><b id='DPz3t'><form id='DPz3t'><ins id='DPz3t'></ins><ul id='DPz3t'></ul><sub id='DPz3t'></sub></form><legend id='DPz3t'></legend><bdo id='DPz3t'><pre id='DPz3t'><center id='DPz3t'></center></pre></bdo></b><th id='DPz3t'></th></span></q></dt></tr></i><div id='DPz3t'><tfoot id='DPz3t'></tfoot><dl id='DPz3t'><fieldset id='DPz3t'></fieldset></dl></div>

      <tfoot id='DPz3t'></tfoot><legend id='DPz3t'><style id='DPz3t'><dir id='DPz3t'><q id='DPz3t'></q></dir></style></legend>
      • <bdo id='DPz3t'></bdo><ul id='DPz3t'></ul>

      <small id='DPz3t'></small><noframes id='DPz3t'>

        如何正确捕获 PHP 异常 (Laravel 5.1)

        时间:2023-09-23
          <bdo id='8NpTT'></bdo><ul id='8NpTT'></ul>

              <legend id='8NpTT'><style id='8NpTT'><dir id='8NpTT'><q id='8NpTT'></q></dir></style></legend>
            1. <i id='8NpTT'><tr id='8NpTT'><dt id='8NpTT'><q id='8NpTT'><span id='8NpTT'><b id='8NpTT'><form id='8NpTT'><ins id='8NpTT'></ins><ul id='8NpTT'></ul><sub id='8NpTT'></sub></form><legend id='8NpTT'></legend><bdo id='8NpTT'><pre id='8NpTT'><center id='8NpTT'></center></pre></bdo></b><th id='8NpTT'></th></span></q></dt></tr></i><div id='8NpTT'><tfoot id='8NpTT'></tfoot><dl id='8NpTT'><fieldset id='8NpTT'></fieldset></dl></div>

              • <tfoot id='8NpTT'></tfoot>
                  <tbody id='8NpTT'></tbody>

                <small id='8NpTT'></small><noframes id='8NpTT'>

                • 本文介绍了如何正确捕获 PHP 异常 (Laravel 5.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一些代码可以进行数据库调用和网络请求,我将它封装在 try/catch 中.问题是我永远无法捕获异常,而且它们似乎不是致命的异常:

                  I have some code that makes db calls and network requests and I have it wrapped in a try/catch. The problem is that I can never catch the exceptions, and they don't appear to be fatal exceptions:

                  try {
                     // make db requests and network calls
                  } catch (Exception $e) {
                     // handle exception
                  }
                  

                  也就是说,我遇到了这样的异常:

                  Namely, I encounter exceptions such as these:

                  [IlluminateDatabaseQueryException] 
                  [PDOException]
                  [InvalidArgumentException] 
                  

                  有没有办法捕捉这些异常?我是否需要对每种可能的异常对象类型进行明确(意味着我必须创建许多尝试/捕获),或者是否有推荐的方法来捕获非致命异常?

                  Is there a way to catch these exceptions? Do I need to be explicit for each possible type of exception object (meaning I must create many try/catches), or is there a recommended way of catching non fatal exceptions?

                  推荐答案

                  确保正确使用命名空间,方法是在控制器顶部包含 Exception 类,如下所示:

                  Make sure you're using your namespaces properly, by including the Exception class at the top of your controller like this:

                   Use Exception; 
                  

                  如果您使用一个类而不提供其命名空间,PHP 会在当前命名空间中查找该类.Exception 类存在于全局命名空间中,因此如果您在某些命名空间代码中执行 try/catch,例如您的控制器或模型,您需要执行以下操作:

                  If you use a class without providing its namespace, PHP looks for the class in the current namespace. Exception class exists in global namespace, so if you do that try/catch in some namespaced code, e.g. your controller or model, you'll need to do:

                  try {
                    //code causing exception to be thrown
                  } catch(Exception $e) {
                    //exception handling
                  }
                  

                  如果你这样做,就不会错过任何异常.

                  If you do it like this there is no way to miss any exceptions.

                  否则,如果您在存储在 AppHttpControllers 中的控制器代码中遇到异常,您的捕获将等待 AppHttpControllersException 对象被抛出.

                  Otherwise if you get an exception in a controller code that is stored in AppHttpControllers, your catch will wait for AppHttpControllersException object to be thrown.

                  这篇关于如何正确捕获 PHP 异常 (Laravel 5.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Laravel 的 Eloquent ORM 中按数据透视表数据排 下一篇:使用 Laravel 4.1 对 UNION 查询进行排序

                  相关文章

                  最新文章

                    <bdo id='oHOW8'></bdo><ul id='oHOW8'></ul>
                • <i id='oHOW8'><tr id='oHOW8'><dt id='oHOW8'><q id='oHOW8'><span id='oHOW8'><b id='oHOW8'><form id='oHOW8'><ins id='oHOW8'></ins><ul id='oHOW8'></ul><sub id='oHOW8'></sub></form><legend id='oHOW8'></legend><bdo id='oHOW8'><pre id='oHOW8'><center id='oHOW8'></center></pre></bdo></b><th id='oHOW8'></th></span></q></dt></tr></i><div id='oHOW8'><tfoot id='oHOW8'></tfoot><dl id='oHOW8'><fieldset id='oHOW8'></fieldset></dl></div>

                    <tfoot id='oHOW8'></tfoot>

                      <small id='oHOW8'></small><noframes id='oHOW8'>

                      <legend id='oHOW8'><style id='oHOW8'><dir id='oHOW8'><q id='oHOW8'></q></dir></style></legend>