php 5提供了基本的异常处理类,可直接使用。
通过异常,抛出错误信息:
try { $error = 'my error!'; throw new exception($error)} catch (exception $e) { echo $e->getmessage();}
我们可以扩展此类,方便我们的使用:
class myexception extends exception{ // 重定义构造器使 message 变为必须被指定的属性 public function __construct($message, $code = 0) { // 自定义的代码 // 确保所有变量都被正确赋值 parent::__construct($message, $code); } // 自定义字符串输出的样式 public function __tostring() { return __class__ . : [{$this->code}]: {$this->message}\n; } public function customfunction() { echo a custom function for this type of exception\n; }}
这个类可以根据你的需要随意扩展使用。
http://www.bkjia.com/phpjc/752479.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/752479.htmltecharticlephp 5 添加了类似于其它语言的异常处理模块。在 php 代码中所产生的异常可被 throw 语句抛出并被 catch 语句捕获。需要进行异常处理的代码都...
