Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Try..Catch and exceptions help.

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
Hi there, I am trying to build some error handling into my current application. I have read around try..catch and exceptions but I am struggling to get the examples I see to work in my real world environment.

Users are allowed to enter the IP address of a second server on the network which has a soap server installed. In the case that the server has been mis-entered or is switched off, I want to try and surpress the SoapClient errors and generate my own.

So far I have got:

Code:
	function createSoapClient() {
		if ($client = new SoapClient("[URL unfurl="true"]http://".$this->getTvIp()."/nagios-soap.php?wsdl",[/URL] array("exceptions" => 0))) {
			return $client;
		} else {
			throw new exception('SOAP client could not be created');
			return false;
		}
	}

and:

Code:
	function makeSoapClient() {
		try {
			$client = $this->createSoapClient();
			if ($client) {
				return $client;
			}
		} catch (exception $e) {
			die('ERROR: '.$e->getMessage());
		}
	}

When I call the function with:

Code:
$client = $db->makeSoapClient();

I get a SOAP error! presumably I need to suppress the soap errors somehow but having read around the soap documentation I cannot work out how to do this. Can anyone give me some pointers please?

G

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
What you are suffering from is the fact that PHP has two error handling mechanisms (3 actually, as some functions just return FALSE in case of an error): errors and exceptions.

You can suppress errors to show up with a '@' character or in the settings. You can handle them in an error handling function, or by checking the last error (should be enabled in PHP.ini) or some result.

What you did is handle exceptions only. I assume that the SoapClient class raises errors instead :-(

Otherwise, there is nothing wrong with your code. Note that "return false;" will never get called, as throwing an exception is already a brute jump out of the try block.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top