您的位置:首页技术文章
文章详情页

PHP基础之预定义异常

浏览:72日期:2022-09-15 10:26:52
1.Exception

(PHP 5 >= 5.1.0)

简介

Exception是所有异常的基类。

方法Exception::__construct — 异常构造函数Exception::getMessage — 获取异常消息内容Exception::getPrevious — 返回异常链中的前一个异常Exception::getCode — 获取异常代码Exception::getFile — 获取发生异常的程序文件名称Exception::getLine — 获取发生异常的代码在文件中的行号Exception::getTrace — 获取异常追踪信息Exception::getTraceAsString — 获取字符串类型的异常追踪信息Exception::__toString — 将异常对象转换为字符串Exception::__clone — 异常克隆2.ErrorException

(PHP 5 >= 5.1.0)

简介

错误异常。

范例

Example #1 使用 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:8Stack 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方法ErrorException::__construct — 异常构造函数ErrorException::getSeverity — 获取异常的严重程度

标签: PHP
相关文章: