Jump to content

[URGENT] Exception - try catch


midnite
 Share

Recommended Posts

http://hk.php.net/manual/en/language.exceptions.php
<?phpecho 'before<BR>';try {  undefined_function ();} catch (Exception $e) {  echo 'err: ', $e->getMessage(), '<BR>';}echo 'after';?>

Isn't Exception $e will catch all errors? i still receive a Fatal error message on the page. And no after is echoed.In fact, i don't care if it can be caught or not. i just want the following of the page to be executed even if that statement fails. What my real code is:

<?php// something before$websrc = file_get_contents($SRC);// something after?>

i use the above example (undefined_function ()) is just want to ensure a fatal error occurs. When file_get_contents() exceeds the connection time limit, it will have a fatal error too.Thanks very very much!!

Link to comment
Share on other sites

Exception $e doesn't catch errors. It only catches Exceptions.But file_get_contents shouldn't throw a fatal error. Most PHP functions don't throw fatal errors. For example, when mysql_connect() fails, only a warning is triggered. Why don't you try this:

<?php$websrc = @file_get_contents($SRC); // Use @ to suppress the error?>

Link to comment
Share on other sites

It will fatally error if it isn't defined (as in PHP 4). If the URL doesn't load it will give a warning.You can always define the function yourself if it doesn't exist.

if (!function_exists('file_get_contents')) {	  function file_get_contents($filename, $incpath = false, $resource_context = null)	  {		  if (false === $fh = fopen($filename, 'rb', $incpath)) {			  trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);			  return false;		  } 		  clearstatcache();		  if ($fsize = @filesize($filename)) {			  $data = fread($fh, $fsize);		  } else {			  $data = '';			  while (!feof($fh)) {				  $data .= fread($fh, 8192);			  }		  } 		  fclose($fh);		  return $data;	  }  }

More about exceptions: http://au2.php.net/manual/en/language.exceptions.php

Link to comment
Share on other sites

Thanks for your replies.file_get_contents() do throw Fatal error!! If i come across it again, i will post the error message.So, i would like to ask, can i skip Fatal error and let the rest of my page to execute?Yes, @ will suppress Fatal error, by just not printing it out. The rest of the page will not be executed also :)

Link to comment
Share on other sites

You could define the function if not set as per the code in my previous post...If the call isn't necessary (somehow) then you can do

!function_exists('file_get_contents') or $websrc = file_get_contents($SRC);

Link to comment
Share on other sites

Thanks for your replies.file_get_contents() do throw Fatal error!! If i come across it again, i will post the error message.So, i would like to ask, can i skip Fatal error and let the rest of my page to execute?Yes, @ will suppress Fatal error, by just not printing it out. The rest of the page will not be executed also :)
That's weird. You should never get a fatal error just for calling an inbuilt PHP function (unless the function is a PHP language construct such as require()). But there is no way to skip a fatal error. Once a fatal error occurs, execution of the script is halted.When the connection timeouts, file_get_contents() should return false and produce an error no higher than an E_WARNING (which you can supress with @). The only way a fatal error would be produced by file_get_contents() is if the function doesn't exist like Synook said.What is your php.ini setting for default_socket_timeout? Also, what is your php.ini setting for max_execution_time?Try to encouter that fatal error again and post it so we can see what's going on.
Link to comment
Share on other sites

What exactly is the fatal error anyway? The best way to handle it is to prevent it from happening it, but we can't do that without knowing what it is.

Link to comment
Share on other sites

Thanks Synook, Silver and boen_robot!In fact, my PHP script uses file_get_contents() to fetch a page. Analyze it. Then generate some javascripts (setTimeout("location.reload()", rand*1000);) to reload the the page in a few seconds.It runs normally for 99% of the time. Yet if file_get_contents() throw a Fatal error, the page will be halted there and no javascripts will be generated then no more reload :)Currently, i guess it is because the resource cannot be fetched within the HTTP time limit, thus cause the PHP max_execution_time exceeds.Silver, here's my configurations. They are defaults. i did not altered them.

default_socket_timeout = 60max_execution_time = 30

It is sad that Fatal error cannot be skipped. If time limit exceed is really the cause of the Fatal error, is it that setting max_execution_time = 0 will solve the problem? Btw, i wont change them at this moment. And once i got that Fatal error again, i must post them here :)

Link to comment
Share on other sites

file_get_contents itself does not cause a fatal error, ever. Something else might, for example the file you're reading might be too large for memory and you're getting a memory error. But file_get_contents itself does not cause a fatal error. And no, of course a fatal error cannot be skipped. That's why they call it fatal.

Return ValuesThe function returns the read data or FALSE on failure.
If you want to test if it worked, test if it returned false.
$data = file_get_contents(...);if ($data === false){  echo "loading failed";}else{  ...}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...