简体   繁体   English

PHP try catch块语法

[英]PHP try catch block syntax

A very simple question from someone without much experience. 一个没有太多经验的人提出的一个非常简单的问题。 The following try catch block has the section, "(Exception $e)" : is this like sql, where $e becomes an alias of Exception? 以下try catch块包含“(Exception $ e)”部分:这类似于sql,其中$ e成为Exception的别名? If so, is this type of alias assignment used elsewhere in php, because I haven't come across it? 如果是这样,这种别名分配类型是否在php的其他地方使用,因为我还没有遇到过? I have searched for hours without being able to find an explanation on the web. 我已经搜索了几个小时,但无法在网上找到解释。

function inverse($x) {
 if (!$x) {
     throw new Exception('Division by zero.');
          }
            else return 1/$x;
          }

      try {
            echo inverse(5) . "<br/>";
            echo inverse(0) . "<br/>";
          } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "<br/>";
          }

            echo 'Hello World';

What you mention is a filter construction. 您提到的是过滤器构造。 It resembles a declaration as known from other, declarative languages. 它类似于其他声明性语言中的声明。 However it has a different meaning in fact. 但是实际上它具有不同的含义。 Actually php does not have the concept of an explicit declaration (which is a shame...). 实际上,php没有显式声明的概念(这很可惜...)。

Take a look at this example: 看一下这个例子:

function my_func($x) {
    try {
        /* do something */
        if (1===$x)
            throw new SpecialException('x is 1.');
        else if (is_numeric($x))  }
            throw new SpecialException('x is numeric.');
        else return $x;
    }
    catch (SpecialException $e) {
        echo "This is a special exception!";
        /* do something with object $e of type SpecialException */
    }
    catch (Exception $e) {
        echo "This is a normal exception!";
        /* do something with object $e of type SpecialException */
    }
}

Here it becomes clear what the construction is for: it filters out by type of exception. 在这里可以清楚知道构造的用途:它按异常类型过滤掉。 So the question which of several catch blocks is executed can be deligated to the type of exception having been thrown. 因此,可以将执行几个catch块中的哪个问题的问题归结为引发的异常类型。 This allows a very fine granulated exception handling, where required. 在需要时,这允许非常精细的异常处理。 Without such feature only one catch block would be legal and you'd have to implement conditional tests for potential exception types in each catch block. 没有这种功能,只有一个catch块是合法的,并且您必须对每个catch块中的潜在异常类型实施条件测试。 This feature makes the code much more readable, although it is some kind of break in the php syntax. 该功能使代码更具可读性,尽管它在php语法中有所突破。

You don't have to, but you can create own exception classes with special behavior and, more important, accepting and carrying more information about what actually happened. 您不必这样做,但是您可以创建自己的具有特殊行为的异常类,更重要的是,可以接受并携带有关实际情况的更多信息。

It's OO PHP. 是OO PHP。 The $e is an instance of the exception object. $ e是异常对象的实例。

$e could easily be labelled anything else, so long as it's referred to thereon when you want to getmessages, etc. $ e可以很容易地被标记为其他任何东西,只要您要获取消息等在它上面被引用即可。

For instance; 例如;

 try {
        echo inverse(5) . "<br/>";
        echo inverse(0) . "<br/>";
      } catch (Exception $oops) {
        echo 'Caught exception: ',  $oops->getMessage(), "<br/>";
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM