Reserved/exceptions-Phpdoc专题
预定义异常
目录
- Exception — Exception
- Exception::__construct — 异常构造函数
- Exception::getMessage — 获取异常消息内容
- Exception::getPrevious — 返回异常链中的前一个异常
- Exception::getCode — 获取异常代码
- Exception::getFile — 创建异常时的程序文件名称
- Exception::getLine — 获取创建的异常所在文件中的行号
- Exception::getTrace — 获取异常追踪信息
- Exception::getTraceAsString — 获取字符串类型的异常追踪信息
- Exception::__toString — 将异常对象转换为字符串
- Exception::__clone — 异常克隆
- ErrorException — ErrorException
- ErrorException::__construct — 构造一个异常(Exception)
- ErrorException::getSeverity — 获取异常的严重程度
- Error — Error
- Error::__construct — 初始化 error 对象
- Error::getMessage — 获取错误信息
- Error::getPrevious — 返回先前的 Throwable
- Error::getCode — 获取错误代码
- Error::getFile — 获取错误发生时的文件
- Error::getLine — 获取错误发生时的行号
- Error::getTrace — 获取调用栈(stack trace)
- Error::getTraceAsString — 获取字符串形式的调用栈(stack trace)
- Error::__toString — error 的字符串表达
- Error::__clone — 克隆 error
- ArgumentCountError — ArgumentCountError
- ArithmeticError — ArithmeticError
- AssertionError — AssertionError
- DivisionByZeroError — DivisionByZeroError
- CompileError — CompileError
- ParseError — ParseError
- TypeError — TypeError
参见 SPL 异常处理
简介
Exception是所有异常的基类。
类摘要
Exception
class Exception {
/* 属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $message
<span
class="initializer"> = "" [, <span
class="methodparam">int $code
<span
class="initializer"> = 0 [, <span
class="methodparam">Throwable $previous
<span
class="initializer"> = null
]]] )
final public string <span class="methodname">getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">getPrevious ( <span class="methodparam">void )
final public mixed getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string <span class="methodname">getTraceAsString ( <span class="methodparam">void )
public string __toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">__clone ( <span class="methodparam">void )
}
属性
message
异常消息内容
code
异常代码
file
抛出异常的文件名
line
抛出异常在该文件中的行号
简介
错误异常。
类摘要
ErrorException
class ErrorException <span class="ooclass"> extends Exception {
/* 属性 */
protected int
$severity
;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $message
<span
class="initializer"> = "" [, <span
class="methodparam">int $code
<span
class="initializer"> = 0 [, <span
class="methodparam">int $severity
<span
class="initializer"> = E_ERROR [, <span
class="methodparam">string $filename
<span
class="initializer"> = __FILE__ [, <span
class="methodparam">int $lineno
<span
class="initializer"> = __LINE__ [, <span
class="methodparam">Exception $previous
<span
class="initializer"> = null
]]]]]] )
final public int <span class="methodname">getSeverity ( <span class="methodparam">void )
/* 继承的方法 */
final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )
public string Exception::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )
}
属性
severity
异常级别
范例
示例 #1 使用<span class="function">set_error_handler函数将错误信息托管至ErrorException
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
/* Trigger exception */
strpos();
?>
以上例程的输出类似于:
Fatal error: Uncaught exception 'ErrorException' with message 'Wrong parameter count for strpos()' in /home/bjori/tmp/ex.php:8
Stack trace:
#0 [internal function]: exception_error_handler(2, 'Wrong parameter...', '/home/bjori/php...', 8, Array)
#1 /home/bjori/php/cleandocs/test.php(8): strpos()
#2 {main}
thrown in /home/bjori/tmp/ex.php on line 8
简介
Error 是所有PHP内部错误类的基类。
类摘要
Error
class Error implements Throwable {
/* 属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $message
<span
class="initializer"> = "" [, <span
class="methodparam">int $code
<span
class="initializer"> = 0 [, <span
class="methodparam">Throwable $previous
<span
class="initializer"> = null
]]] )
final public string <span class="methodname">getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">getPrevious ( <span class="methodparam">void )
final public mixed getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string <span class="methodname">getTraceAsString ( <span class="methodparam">void )
public string __toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">__clone ( <span class="methodparam">void )
}
属性
message
错误消息内容
code
错误代码
file
抛出错误的文件名
line
抛出错误的行数
简介
ArgumentCountError 当传递给用户定义的函数或方法的参数太少时被抛出。
类摘要
ArgumentCountError
class ArgumentCountError <span class="ooclass"> extends TypeError {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}
简介
ArithmeticError 当执行数学运算时发生错误时被抛出。PHP 7.0 these errors include attempting to perform a bitshift by a negative amount, and any call to intdiv that would result in a value outside the possible bounds of an integer.
类摘要
ArithmeticError
class ArithmeticError <span class="ooclass"> extends Error {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}
简介
AssertionError 在函数 <span class="function">assert 断言失败时被抛出。
类摘要
AssertionError
class AssertionError <span class="ooclass"> extends Error {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}
简介
DivisionByZeroError 当除数为零时被抛出。
类摘要
DivisionByZeroError
class DivisionByZeroError <span class="ooclass"> extends ArithmeticError {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}
简介
CompileError 是针对一些编译错误抛出的,之前是会发出致命错误。
类摘要
CompileError
class CompileError <span class="ooclass"> extends Error {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}
简介
ParseError 当解析 PHP 代码时发生错误时抛出,比如当 <span class="function">eval被调用出错时。
Note: 从 PHP 7.3.0 开始,<span class="classname">ParseError 继承自 <span class="classname">CompileError。之前的版本,则继承自 <span class="classname">Error。
类摘要
ParseError
class ParseError <span class="ooclass"> extends CompileError {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}
简介
有三种情况会抛出 <span class="ooclass">TypeError。第一种,传递给函数的参数类型与函数预期声明的参数类型不匹配;第二种,函数返回的值与声明的函数返回类型不匹配;第三种,调用 PHP 内置函数时,传递了非法的数字参数(仅限在严格模式下 / strict mode)。
类摘要
TypeError
class TypeError <span class="ooclass"> extends Error {
/* 继承的属性 */
protected string
$message
;
protected int
$code
;
protected string
$file
;
protected int
$line
;
/* 继承的方法 */
final public string <span class="methodname">Error::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Error::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Error::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Error::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Error::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Error::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Error::getTraceAsString ( <span class="methodparam">void )
public string Error::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Error::__clone ( <span class="methodparam">void )
}