在PHP7中抛出异常进行验证?

时间:2016-05-24 14:42:04

标签: php validation exception exception-handling php-7

我正在更新我刚刚完成的框架。我从社区得到了一些反馈,并决定构建一个可移植的MVC框架来容纳我的软件包。

我正在使用路由器,当我向框架提供它应该获得的URL时,它的工作正常。我使用throw new Exception("MyMessage")来验证更加面向对象的URL并且更加简单。

然而,这是我第一次尝试这个,而且似乎我的catch块没有捕获错误。

具体来说,只要try块中出现错误,我就希望它在catch块中触发我的一些代码。 这是完整的try/catch块...

try
      {
        if (isset($url[0]) && isset($url[1]) && isset($url[2]) && $url[0] . $url[1] . $url[2] != "")
        {
          //Figure out if we're handling a package, or working in the package manager.
          if ($url[0] != "ignition" && $url[0] != "")
          {
            if (!file_exists("../packages/" . $url[0]))
            {
              throw new Exception($manager_error = "Package \"" . $url[0] . "\" not found.");
            }
            else
            {
              $this->package = $url[0];
              $this->path = "../packages/$this->package/controllers/";
            }
            unset($url[0]);
          }
          elseif ($url[0] == "")
            throw new Exception("No package name provided.");
          //Check if controller exists.
          if ($url[1] != "")
          {
            //Check if controller file exists.
            if (file_exists($this->path . $url[1] . ".php"))
            {
              $this->controller = $url[1];
              require_once($this->path . "$this->controller.php");
              unset($url[1]);
              //Check that controller exists in file.
              if (class_exists($this->controller))
              {
                $this->controller = new $this->controller;
                //Check that method exists in controller.
                if ($url[2] != "")
                {
                  if (method_exists($this->controller, $url[2]))
                  {
                    $this->method = $url[2];
                    unset($url[2]);
                    if (!empty($url))
                      $this->params = array_values($url);
                  }
                  else
                    throw new Exception("Method \"" . $url[2] . "\" not found in controller \"" . get_class($this->controller) . "\".");
                }
                else
                  throw new Exception("No method name provided.");
              }
              else
                throw new Exception("Controller \"$this->controller\" not contained in it's file.");
            } else {
              throw new Exception("Controller file \"" . $url[1] . ".php\" not found.");
            }
          }
          else
            throw new Exception("No controller name provided.");
        }
        else
        {
          //Check if we have one or more arguments, but less than the three
          //we need for a complete route.
          if (!isset($url[0]))
            $url[0] = "";
          if (!isset($url[1]))
            $url[1] = "";
          if (!isset($url[2]))
            $url[2] = "";
          if ($url[0] . $url[1] . $url[2] != "") {
            if ($url[0] == "")
              throw new Exception("No package name provided.");
            elseif ($url[1] == "")
              throw new Exception("No controller name provided.");
            elseif ($url[2] = "")
              throw new Exception("No method name provided.");
          }
        }
      }
      catch (Exception $e)
      {
        unset($url);
        $this->manager_error = $e->getMessage();
        $this->package = "ignition";
        $this->controller = "ignition";
        $this->method = "notFound";
        $this->path = "../package_manager/controllers/";
        $this->params[] = $this->manager_error;
        require_once($this->path . "$this->controller.php");
        $this->controller = new $this->controller;
      }

我尝试制作一个非常简单的版本,其中我只是回显了一些文本并在地址栏中放置了一些错误的URL以尝试从catch块获取SOMETHING,但我没有成功。

我还想提一下,这是在类的构造函数中,如果这会产生任何差别。

我在这里做错了什么,如何更改此脚本以获取捕获的异常?

1 个答案:

答案 0 :(得分:0)

命名空间中Exception的一个常见问题是你可能正在抛出一个扩展的孩子。一个例子是

namespace test;

class Class {
    public function __construct(){
        try {
            throw new Exception('Msg');
        } catch(Exception $err) {
        }
    }
}

所以在这个例子中,我们正在抛弃\test\Exception。命名空间改变了我们抛出的异常类。这出错的地方就是这个

try {
    new \BadClass(); // throws BadException
} catch(Exception $err) {
}

这不会抛出\test\Exception,但你的类型提示是期待的。因此,这将产生致命错误,因为catch与类型提示中的正确类不匹配。有一种简单的方法来解决这个问题

catch(\Exception $err)

现在,您的catch可以捕获所有异常,因为根据定义,它们都扩展了基类。 \告诉PHP使用根Exception类。