php: try catch / error freezes server script - php

I am running a cronjob script which completely freezes within a try / catch statement (which is inside a while loop).
The catch phrase is working as it's logging the error (which i put there) but then it freezes completely without any output.
after that i only put echo "error"; inside the catch pharse but it does not output and freezes again.
when adding die; it prompts and the script stops as expected - but i want to catch the error and continue the script.
while($rs = $res->fetch_assoc())
{
try
{
$rc = new c_movie();
$result = $rc->search();
} catch(Throwable $t)
{
echo "ERROR!!!!"; // freezing here, does not echo
}
}
any idea what could be causing the script to freeze?
thanks

I finally found out by myself .. my code was using get_headers() which caused the freeze when trying to load a URL which had 404. Seems like the server is somekind of caching it and freezes and won't trigger the exception. Very Strange ..

Related

PHP CodeIgniter Errors not stopping execution of Try Blocks

I'm using CodeIgniter and am trying to execute code in a try/catch block with the idea that errors will stop execution of the code after the error until the catch block is reached, as you would normally think it would work.
However on encountering PHP Errors, the code is continuing. This is causing a database transaction complete command to execute which is .... very bad if there's an error and all of the instructions weren't carried out properly. For example, I have this code which is executed in an ajax request:
// start transaction
$this->db->trans_start();
try {
$this->M_debug->fblog("firstName=" . $triggerOpts->{'firstXXXName'});
$data = array("test_col" => 123);
$this->db->where("id", 4);
$this->db->update("my_table", $data);
// if got this far, process is ok
$status = "process_ok";
// complete transaction
$this->db->trans_complete();
} catch (Exception $ex) {
// output the error
$this->M_debug->logError($ex);
}
In this code, I'm trying to execute a database update as part of a transaction.
My call to $this->M_debug->fblog() is designed to just log a variable to PHP Console, and I've deliberately tried to log a variable that does not exist.
This causes a PHP error, which I guess is a fatal error, and the desired result is that the code after the log commands fails, and the transaction does not complete. However after this error, despite reporting the PHP error in Chrome console, the code keeps right on executing, the database is updated and the transaction is completed. Would appreciate any help in how i could stop this from happening.
Thanks very much, G
EDIT --
As requested heres fblog(), it's simply a Chrome console log request of a variable
public function fblog( $var ) {
ChromePhp::log( $var );
}
Assuming you're using PHP 7.0 or higher, you can catch PHP errors as well as exceptions, however you need to catch Error or the parent Throwable type rather than Exception.
try {
...
} catch (Throwable $ex) {
//this will catch anything, including Errors and Exceptions
}
or catch them separately if you want to do something different for each of them...
try {
...
} catch (Exception $ex) {
//this will catch Exceptions but not errors.
} catch (Error $ex) {
//this will Errors only
}
Note that if you're still only PHP 5.x, the above won't work; you can't catch PHP errors in older PHP versions.

PHP file_get_contents is asynchronous?

I read that file_get_content is synchronous, but when I tried the code below I dont' think so :
$url = "http://foo.com";
$a = array("file11.php", "file2.php", "file3.php");
foreach ($a as $file)
{
$final = $url . "/" . $file;
print "Calling $final ...";
$res = file_get_contents($final);
if ($res)
print "OK";
else
print "ERR!";
print "<br>";
}
Each file executes some complex tasks, so I know the minimal excution time of any script, but this code runs very fastly and seems not to wait each request ! How can I wait for each file request?
Thanks :)
The above code is definitely synchronous. So if you say that the code exits after a few seconds, while it should be a lot longer, then you probably have a problem with the code.
Try to wrap this code in a try {} catch. And print the error. See what it says.
Try { code here } catch (Exception $e) { }
Also, most default settings in the php.ini for MAX_EXECUTION for a script is 30 seconds. After that it will exit on a fatal timeout error too. Check the setting in your php.ini and adjust it to your needs.
Edit:
Gathering your comments, I now assume you are trying to execute the php files you are referring to. This makes your question very confusing and the tags just wrong.
The code you use in your example only reads the contents of the file, so it's not executing anything. Which explains why it returns so fast, while you expect it to take a while.
If you want to execute the referred php files, approach it like this:
Include_once( $final );
Instead of opening the contents.

Try/Catch breaks PHP script

SOLUTION: I've been working on the iOS side of it at the same time and must have got the languages switched up. Instead of the $, I put *.
I am trying to get a try{} catch{} working in PHP. My code works when I remove the try{} catch{}. Once I put it back in, it breaks my script. I even tried making it empty in both the try{} catch{}, but it still crashes my script.
try {
}
catch (Exception *e) {
}
Is there a reason the try{} catch{} would cause the script to crash? When I run it in my browser it just shows a white screen.
I even went and made another empty PHP file and put this code in without the if statement. And still, it doesn't work. The page is still white. I had it echo in the try.
Your catch declaration is incorrect.
catch (Exception *e) {
Should be.
catch (Exception $e) {
The inaccurate code would cause a parse error, thus preventing the script from running at all, and producing a white screen.
Maybe possibilities catch is not getting the object values. You should use it as:
try
{
some statement....
}
catch(Exception e)
{
some statement...
}

How to prevent a try-catch error from stopping execution

I've read this thread: php: catch exception and continue execution, is it possible?
Every answer suggests that a try catch will continue executing the script. Here is an example where it doesn't:
try{ $load = #sys_getloadavg(); }
catch (Exception $e){ echo 'Couldn\'t find load average.<br>'; return false; }
I'm running it on xampp on windows, which could be why it errors (it gives a Call to undefined function sys_getloadavg() error when the # is removed), but that isn't the issue in question. It could be any function that doesn't exist, isn't supported or fails - I can not get the script to continue executing.
Another example is if there is a syntax error in the try, say I'm including an external file and parsing it as an array. This also produces an error and stops executing.
Is there any brute force way to continue the script running, regardless of what fails in the try?
Unlike other languages, there's a difference in PHP between exceptions and errors. This would be like a compile error in other languages. that require declaration files. You can't catch or ignore Fatal errors like a function not exisiting. You can test for existence before using though:
if( function_exists('sys_getloadavg') {
try{ $load = #sys_getloadavg(); }
catch (Exception $e){ echo 'Couldn\'t find load average.<br>'; return false; }
}

PHP script dies after each error

Im trying to make my script output the error on screen but the error keeps outputting into error_log and killing the script,
This is my current code
try{
$db->query("SELECT `test`.`test` FROM `test` WHERE `test`.`test` = test");
echo("no work?");
}catch(PDOException $er){
print("Still dont work");
}
$db->query... returns a error like it should but the script dies there, outputs into the error_log and wont finish it like i would like it to.
Can anyone help?
Just to sum up some of the responses i got, it is not the actual die() function that is killing the script, it's the error it's self at $db->query().
As the name would suggest, die() makes your script die (i.e, exit) after printing the message you pass it.
If you just want it to print the error, use print() instead of die().

Categories