skaterdav85 Posted January 8, 2010 Share Posted January 8, 2010 How would I put a throw in my try statement? I'm not really sure where it fits in that try block. I want it so that if there is any error, it throws. <?php$params = "SELECT SKILL FROM DB WHERE ID = '12345'";$wsdl = "someWSDLfile.wsdl";try { $currentAP = new SoapClient($wsdl); $result = $currentAP->execute($params); print_r($result);}catch (Exception $e) { echo 'no connection';}?> Link to comment Share on other sites More sharing options...
Synook Posted January 8, 2010 Share Posted January 8, 2010 You put in inside the catch block. See example #2 on the manual page - http://php.net/manual/en/language.exceptions.php. Link to comment Share on other sites More sharing options...
MrAdam Posted January 8, 2010 Share Posted January 8, 2010 You put in inside the catch block. See example #2 on the manual page - http://php.net/manual/en/language.exceptions.php.That would be for a nested exception?I think the big dave's after:<span class="postcolor"><?php $params = "SELECT SKILL FROM DB WHERE ID = '12345'"; $wsdl = "someWSDLfile.wsdl"; try { $currentAP = new SoapClient($wsdl); $result = $currentAP->execute($params); throw new Exception('SOME_ERROR'); } catch (Exception $e) { // $e->getMessage() would equal 'SOME_ERROR' echo 'no connection'; } ?> Of course you'll want to only throw the exception if something goes wrong. Link to comment Share on other sites More sharing options...
skaterdav85 Posted January 8, 2010 Author Share Posted January 8, 2010 but how do i detect if something goes wrong? I'm not sure what error could happen. I know that if it doesnt work, I get a fatal error.I want something like: if(fatal error) { throw new Exception('there was an error');} How do i translate this into actual code? Link to comment Share on other sites More sharing options...
Synook Posted January 9, 2010 Share Posted January 9, 2010 You can't catch or in any way negate fatal errors (as in E_ERROR) (after all, they are fatal ). Exception handling is only for classes that implement it, and is not related to the legacy error level system. In the original code, everything inside the try {} block is watched for exceptions, but if a piece of code within it still tried to do something fatal, the code will still end. That would be for a nested exception?I think the big dave's after...I thought he wanted:<?php$params = "SELECT SKILL FROM DB WHERE ID = '12345'";$wsdl = "someWSDLfile.wsdl";try { $currentAP = new SoapClient($wsdl); $result = $currentAP->execute($params); print_r($result);}catch (Exception $e) { throw new Exception(); //i.e., pass it on}?> Link to comment Share on other sites More sharing options...
skaterdav85 Posted January 9, 2010 Author Share Posted January 9, 2010 so would it not make sense to use a try...catch for consuming this web service? Link to comment Share on other sites More sharing options...
Synook Posted January 9, 2010 Share Posted January 9, 2010 I don't know, is it supposed to throw an exception if the error you were trying to anticipate occurs? Link to comment Share on other sites More sharing options...
skaterdav85 Posted January 9, 2010 Author Share Posted January 9, 2010 so let me get this straight. if i have a fatal error in my try block, my script will automatically execute what is in my catch block? ...and I would use Exception handling in my try block for anticipated errors?In other words, can I just do the following? Is this valid? try { //some code here that might produce a fatal error} catch { echo 'some error occured';} Link to comment Share on other sites More sharing options...
boen_robot Posted January 9, 2010 Share Posted January 9, 2010 so let me get this straight. if i have a fatal error in my try block, my script will automatically execute what is in my catch block? ...and I would use Exception handling in my try block for anticipated errors?In other words, can I just do the following? Is this valid?try { //some code here that might produce a fatal error} catch { echo 'some error occured';} Not exactly. If the try block throws an Exception, the catch block will execute. If it triggers a fatal error, the whole script will end. If you want to do something on fatal errors, right before the script ends, you must register an error handling function with set_error_handler(). Link to comment Share on other sites More sharing options...
skaterdav85 Posted January 9, 2010 Author Share Posted January 9, 2010 what would be an example of an unanticipated Exception that could occur in the try block? ...and what does a fatal error mean? Link to comment Share on other sites More sharing options...
Synook Posted January 10, 2010 Share Posted January 10, 2010 Exceptions are there to be anticipated - that's why you create a try...catch block!A fatal error is a syntax or runtime error that cannot be recovered from and therefore causes the script to halt execution. These differ from warnings, which are errors that can be recovered from, and notices, which are minor programming issues that do not affect the outcome of the script. <?php class Test { function trigger_exception() { throw new Exception("Exception!"); } function trigger_error() { require("non-existant-file"); } } $test = new Test(); try { $test->trigger_exception(); } catch (Exception $e) { echo "An exception occured."; } try { $test->trigger_error(); } catch (Exception $e) { echo "This won't appear - the script will just crash."; }?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.