如果一个异常没有被捕获,而且又没用使用 set_exception_handler() 作相应的处理的话,那么 PHP 将会产生一个严重的错误,并且输出 Uncaught Exception ... (未捕获异常)的提示信息。
先来看一下PHP内置异常类的基本属性和方法。(不包括具体实现)
<?php
/**
* Exception.php
*
* PHP5内置的异常类的属性与方法
* 以下这段代码只为说明内置异常处理类的结构,它并不是一段有实际意义的可用代码。
*/
class Exception{
protected $message = 'Unknown exception'; // 异常信息
protected $code = 0; // 用户自定义异常代码
protected $file; // 发生异常的文件名
protected $line; // 发生异常的代码行号
function __construct($message = null, $code = 0);
final function getMessage(); // 返回异常信息
final function getCode(); // 返回异常代码(代号)
final function getFile(); // 返回发生异常的文件名
final function getLine(); // 返回发生异常的代码行号
final function getTrace(); // backtrace() 数组
final function getTraceAsString(); // 已格成化成字符串的 getTrace() 信息
//可重载的方法
function __toString(); // 可输出的字符串
}
?>例子如下:
包含文件错误抛出异常
<?php
// 错误的演示
try {
require ('test_try_catch.php');
} catch (Exception $e) {
echo $e->getMessage();
}
// 正确的抛出异常
try {
if (file_exists('test_try_catch.php')) {
require ('test_try_catch.php');
} else {
throw new Exception('file is not exists');
}
} catch (Exception $e) {
echo $e->getMessage();
}更多教程:《php教程》
Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务