Error handling using try catch block in PHP -


this question has answer here:

before raising question referred below answers

when use try catch blocks

as not getting clarity on this, raising question. please don't close question duplication.

i have below php script upto date currency value. since using api, guess there may downtime or similar. have assign default value if api not providing.

i know there other methods isset() , !empty()

but use try{} catch(){}

below script doesn't have error works fine.

<?php try {     $return = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22aedusd%22%29&env=store://datatables.org/alltableswithkeys");     $xml=simplexml_load_string($return) or die("error: cannot create object");      $coversionrate = (string)$xml->results->rate->rate;     echo '<pre>'; print_r($coversionrate); } catch (exception $e) {     echo 'caught exception: ',  $e->getmessage(), "\n"; } ?> 

now, have made mistake in api url, should go catch() block , should give soft message. throws warning message.

can use try(){..} catch() {..} in scenario or not?

try..catch handles exceptions. none of code show ever throw exception. catch block never invoked. errors else in php not exceptions. errors can silenced (using @ or global error_reporting settings) or handled globally using defined error handler.

try..catch isn't applicable code, want be.


having said that, can use custom error handler turn error exception. that's errorexception class for. see example in manual. enable use try..catch everything. arguably isn't bad idea, since php's split error/exception mechanism is weird.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -