类文档

Exception

💡 云策文档标注

概述

本文档介绍了 WordPress 中用于 HTTP 请求的 Exception 类,该类继承自 PHPException,提供了处理异常的类型和数据功能。

关键要点

  • Exception 类扩展了 PHPException,用于 HTTP 请求相关的异常处理。
  • 类包含两个受保护属性:type(字符串类型)和 data(混合类型),分别表示异常类型和关联数据。
  • 提供了构造函数 __construct() 用于创建异常,以及方法 getType() 和 getData() 用于获取异常类型和数据。

代码示例

Source class Exception extends PHPException {
    protected $type;
    protected $data;

    public function __construct($message, $type, $data = null, $code = 0) {
        parent::__construct($message, $code);
        $this->type = $type;
        $this->data = $data;
    }

    public function getType() {
        return $this->type;
    }

    public function getData() {
        return $this->data;
    }
}

📄 原文内容

Exception for HTTP requests

Methods

Name Description
Exception::__construct Create a new exception
Exception::getData Gives any relevant data
Exception::getType Like Exception::getCode(), but a string code.

Source

class Exception extends PHPException {
	/**
	 * Type of exception
	 *
	 * @var string
	 */
	protected $type;

	/**
	 * Data associated with the exception
	 *
	 * @var mixed
	 */
	protected $data;

	/**
	 * Create a new exception
	 *
	 * @param string $message Exception message
	 * @param string $type Exception type
	 * @param mixed $data Associated data
	 * @param integer $code Exception numerical code, if applicable
	 */
	public function __construct($message, $type, $data = null, $code = 0) {
		parent::__construct($message, $code);

		$this->type = $type;
		$this->data = $data;
	}

	/**
	 * Like <a href="https://developer.wordpress.org/reference/classes/Exception/getCode/">Exception::getCode()</a>, but a string code.
	 *
	 * @codeCoverageIgnore
	 * @return string
	 */
	public function getType() {
		return $this->type;
	}

	/**
	 * Gives any relevant data
	 *
	 * @codeCoverageIgnore
	 * @return mixed
	 */
	public function getData() {
		return $this->data;
	}
}