I'm not sure if I am using try/catch incorrectly but here is my example,

try{

$xml = file_get_contents("http://www.someotherdomain.com/file.txt");

} catch(Exception $e){

echo "Load Failed\n";

}

The way I see it, try catch is good for this since there is a chance the resource is external and you cant be sure that its accessible. What I don't understand is, why do you still get the "failed to open stream: HTTP request failed!" warning when the code is within the try/catch block

Isn't the try/catch supposed to stop the errors being displayed then go to the catch block. Then you can choose to do what you want with the error message inside the Exception object.

What seems to happen is you get the warning and it goes to the catch...

    Damn, Seems I messed up in my example testing somehow.

    It seems file_get_contents doesn't throw and exception, so try/catch doesn't do anything for it at all. That's quite annoying...

      5 days later

      Could you do this w/out the exceptions?

      if(!$xml = file_get_contents('file.txt'))
      {
      	print 'Load Failed.';
      }

      If you really wanted exceptions...

      function myFetchContents($file)
      {
      	if(!$xml = file_get_contents($file))
      	{
      		throw new Exception('Load Failed');
      	}
      }
      
      // to call
      
      try {
      	myFetchContents('file.txt');
      }
      catch(Exception $e)
      {
      	print $e->getMessage();
      }

        Of course you can, but that's not really my point. I was playing around and trying out exceptions.

        I just find it funny that some errors/fails will trigger an exception and some don't.

        file_get_contents returns a warning, but doesn't cause an exception. So, say you use Exception handling for whatever other reason, you then need to do it manually with file_get_contents (among others)

          I think that's because exception handling was introduced in PHP5 as one of their OOP features. The try/catch is just a complicated if statement, and I don't see much use for it if you're trying to use instead of an if statement. It's much more beneficial if you are using objects that throw exceptions so the client code knows something went wrong, but the object itself doesn't handle how to handle them. They provide substantial debugging information and can really aid in an object-oriented environment.

            4 years later

            Hey,

            I registered specifically to help you out. The code you is perfect the only thing you're missing is the "@" symbol before $xml variable to suppress the error being raised file_get_contents();

            Also, there is rule in PHP: unlike other programming languages, such as Java and C# where errors are thrown automatically by the code in PHP you have to manually throw the exception if something fails.

            file_get_contents() - this method returns FALSE on fail and TRUE on success. All you have to do is check the return result of this method to decide if something went wrong.

            Here is an example I posted a while ago: http://pc-technic.blogspot.com/2010/10/php-filegetcontents-exception-handling.html

            I hope it helps.

              hereTemporary;10978720 wrote:

              Hey,

              I registered specifically to help you out....

              Kind of you to do so, though the fact that this thread is about 3-1/2 years old means it's likely the OP won't be checking up on it. :evilgrin:

              • skug replied to this.
                8 years later

                NogDog might not have helped OP but it did show up on a google search I did on the subject some 8 years later and helped me out 🙂 Also sorry for necroposting....

                  Now if we could only find that bump-reporting thread ... 😃

                  Anyways, welcome to PHPBuilder, skug!! 🙂

                    At the risk of further perpetuating this zombie thread...

                    First, you should be checking the result of file_get_contents. According to the documentation, this function returns FALSE on failure. So something like this:

                    $contents = file_get_contents("https://trumpgolfcount.com");
                    if ($contents === FALSE) {
                        // failed! do something here
                    }

                    Second, try/catch is a powerful way to handle errors and I use it extensively in my coding now. Not every native PHP function will throw an exception -- the old ones certainly won't -- but this shouldn't stop you from using exceptions in your own code. I had another thread that discusses error handling in detail and folks had some pretty helpful input. Exceptions are useful if you want your function to indicate an error condition separately from whatever value it returns. This can eliminate a lot of if/else statements because you don't have to check the value returned by every function to see if an error happened. You can also use multiple catch statements to allow much more highly tuned responses in your code. Think of your catch blocks as switches or multiplexors where you can switch the train of code execution onto whatever track is most appropriate.

                      Write a Reply...