0

Before raising the question I referred below answers

When to use Try Catch blocks

As I am not getting clarity on this, I am raising this question. So please someone don't close this question as it is duplication.

I have below PHP script to get upto date currency value. Since I am using an API, I guess there may be some downtime or similar. So I have to assign a default value if API is not providing.

I know there are some other methods like isset() and !empty()

But I would like to use try{} catch(){}

Below script doesn't have any error it 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, I have made a mistake in API URL, so it should go catch() block and should give soft message. But it throws warning message.

Can I use try(){..} catch() {..} in this scenario or not?

Community
  • 1
  • 1
Butterfly
  • 2,418
  • 5
  • 30
  • 49
  • try/catch will catch errors that occur in PHP or it's modules (i.e. mysqli not able to connect to database server), not on some external service. file_get_contents receives a response from Yahoo API, it does not know whether it's an error or not. It's up to you to parse the response and figure out whether the request was successful. – martynasma Jul 14 '15 at 12:38
  • @martynasma you are right, as I am passing invalid url into file get contents method, it could not load the URL and PHP throws error right, that error cant be handled? – Butterfly Jul 14 '15 at 12:43
  • I suppose file_get_contents function does not throw exceptions then. "Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions. However, errors can be simply translated to exceptions with ErrorException." as this page says: http://php.net/manual/en/language.exceptions.php – martynasma Jul 14 '15 at 12:47

1 Answers1

2

try..catch handles exceptions. None of the code you show will ever throw an exception. So the catch block will never be invoked. Errors are something else in PHP which are not exceptions. Errors can only be silenced (using @ or global error_reporting settings) or handled globally using a defined error handler.

try..catch simply isn't applicable to your code, however much you want it to be.


Having said that, you can use a custom error handler to turn any error into an exception. That's what the ErrorException class is for. See its example in the manual. That would enable you to use try..catch for everything. Arguably this isn't a bad idea, since PHP's split error/exception mechanism is weird.

deceze
  • 491,798
  • 79
  • 706
  • 853
  • using @ is standard way to silence the error? So for I have thought it is immoral way to hide the error? – Butterfly Jul 15 '15 at 05:02
  • I never said it's a good idea, only that it's possible. – deceze Jul 15 '15 at 05:26
  • The other question pretty much does answer the question IMO. Also, your answer isn't really much of an answer, if anything you should post this as a comment. You're just pointing people towards the other question, which really marking as a duplicate is for. – deceze Jul 15 '15 at 11:54
  • okay :-( thanks for your time – Butterfly Jul 15 '15 at 11:57