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
https://forums.phpfreaks.com/topic/81113-exception-not-being-caught-by-catch/
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}";
}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.