Jump to content

exception not being caught by catch


jordanwb

Recommended Posts

<?
try
{
$this->html = file_get_contents ($this->file);
}
catch (Exception $e)
{
            
}
?>

 

This section of code is in a class and $this->file is defined. It has the value of "http://google.ca/url?sa=p" (no quotes). file_get_contents throws the following exception:

 

Warning: file_get_contents(http://google.ca/url?sa=p) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 204 No Content in C:\xampp\htdocs\jproxy\includes\file_parser.php on line 27

 

So what I'm wondering is how come catch isn't catching the exception?

Link to comment
Share on other sites

Unlike most languages, PHP generated exceptions are not caught by catch. You would need to use something like...

 

<?php

try {
  if (!$this->html = file_get_contents ($this->file)) {
    throw new exception("failed to open {$this->file}");
  }
} catch (Exception $e) {
  echo "Exception caught: {$e->message}";
}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.