实例代码1: try { $this-soapclientobj = new soapclient(self::url . '?wsdl', array('connection_timeout' = self::connection_timeout)); } catch (exception $e) { throw new exception($e-getmessage(), $e-getcode()); } 实例代码2: ?php header (
实例代码1:
try {
$this->soapclientobj = new soapclient(self::url . '?wsdl', array('connection_timeout' => self::connection_timeout));
} catch (exception $e) {
throw new exception($e->getmessage(), $e->getcode());
}
实例代码2:
__getfunctions());//获取服务器上提供的方法
echo ;
echo 'soap服务器提供的type:';
print_r($client->__gettypes());//获取服务器上数据类型
echo
;
echo '执行getguidnode的结果:';
//查询中国北京的天气,返回的是一个结构体
$result=$client->getweather(array('cityname'=>'beijing','countryname'=>'china'));
echo $result->getweatherresult;//显示结果
?>
运行结果:
对try和catch进行实例说明
eg:
1) {
throw new exception(value must be 1 or below);
}
return true;
}
//在 try 代码块中触发异常
try {
//if the exception is thrown, this text will not be shown echo 'if you see this, the number is 1 or below';
checknum(2);
}catch(exception $e){
//捕获异常
echo 'message: ' .$e->getmessage();
}
?>
上面代码将获得类似这样一个错误:
message: value must be 1 or below
例子解释:上面的代码抛出了一个异常,并捕获了它:
创建 checknum() 函数。它检测数字是否大于 1。如果是,则抛出一个异常。在 try 代码块中调用 checknum() 函数。checknum() 函数中的异常被抛出catch 代码块接收到该异常,并创建一个包含异常信息的对象 ($e)。通过从这个 exception 对象调用 $e->getmessage(),输出来自该异常的错误消息不过,为了遵循“每个 throw 必须对应一个 catch”的原则,可以设置一个顶层的异常处理器来处理漏掉的错误。