您的位置:首页 > 编程语言 > PHP开发

PHP5的异常处理机制[12]--异常的传递、重掷异常

2004-08-30 22:01 686 查看
如果我们已经触发了一些在发生时无法马上处理的异常,有一个很好的解决方案—将处理异常的责任交回给调用当前方法的代码,也就是在catch语句中再次抛出异常(重掷异常)。这将使异常沿着方法的调用链向上传递。
index_php5_5.php
<?php

//PHP5

class
RequestHelper
{

private
$request
=array();

private
$defaultcmd
=
'defaultcmd'
;

private
$cmdstr
;


function
__construct
(
$request_array
=
null
){

if(!
is_array
(
$this
->
request
=
$request_array
)){

$this
->
request
=
$_REQUEST
;

}

}


function
getCommandString
(){

return(
$this
->
cmdstr
?
$this
->
cmdstr
:(
$this
->
cmdstr
=
$this
->
request
[
'cmd'
]));

}


function
runCommand
(){

$cmdstr
=
$this
->
getCommandString
();

try{

$mgr
=new
CommandManager
();

$cmd
=
$mgr
->
getCommandObject
(
$cmdstr
);

$cmd
->
execute
();

}catch(
IllegalCommandException$e
){

error_log
(
$e
->
getMessage
());

if(
$cmdstr
!=
$this
->
defaultcmd
){

$this
->
cmdstr
=
$this
->
defaultcmd
;

$this
->
runCommand
();

}else{

throw
$e
;

}

}catch(
Exception$e
){

throw
$e
;

}

}

}


$helper
=new
RequestHelper
(array(
cmd
=>
'realcommand'
));

$helper
->
runCommand
();

?>

以上我们使用了
RequestHelper
类中
一段客户代码。
RequestHelper
用来处理用户提供的请求数据。在构造函数中我们接受一个用来
debug
的数组。如果没有接受到这个数组,类将使用
$_REQUEST
数组。无论哪个数组被使用,它都将分配给名为
$request
的变量。客户代码通过给出一个
request
数组的
cmd
元素,告知它想要执行的
command
getCommandString()
方法则测试一个名为
$cmdstr
的属性。如果它是空的,则方法将$request中的cmd元素的内容分配给$cmdstr,并返回值。如果不是空的,方法直接返回$cmdstr属性的值。通过这样的机制,command字符串可以在
RequestHelper
类中被覆写。

在最后我们将除
IllegalCommandException
外的所有异常对象都将交给更高一级的类来延后处理。我们在最后一个
catch
语句中再次抛出异常。

}catch(
Exception$e
){

throw
$e
;

}

如果我们捕捉到一个
IllegalCommandException
异常,我们首先尝试去调用
一个默认的
command
我们通过将
$cmdstr
属性设置为与
$defaultcmd
等值,并重复地调用
runCommand
方法。如果
$cmdstr
$defaultcmd
字符串已经相等,我们没有什么需要做的,则重掷异常。

事实上在Zend引擎II将会自动重掷所有未匹配的异常,所以我们可以省略最后一个catch语句。这是
CommandManager::getCommandObject()
最后一行:
return
$class
->
newInstance
();


这里要注意两个问题:

首先,我们假设CommandManager类的构造函数不需要参数。在本文中我们不讨论需要参数的情况。其次,我们假设command类(这里是指我们自定义的realcommand)可以被实例化。如果构造函数被声明为private,这个语句将抛出一个
ReflectionException
对象。如果我们没有在
RequestHelper
中处理异常,则这个异常将被传递到调用
RequestHelper
的代码中。如果一个异常被隐性地抛出,你最好在文档中说明一下,或者手动地抛出这个异常
--
这样其他的程序员使用你的代码时容易处理可能发生的异常情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: