Link to home
Start Free TrialLog in
Avatar of duncanb7
duncanb7

asked on

Fatal Error make my php quit right away

Dear experts,
For Error handling, for example, in VBA, we can have "On Error Resume Next" or "On Error Goto" to
let the program still in running even the error happen at exact line number.
In Javascript, that is also easily to understand the error handling from try and catch{}, if error happen
in try {} and then goto catch();

But for my php I am trying using try and catch{} that is NOT exactly same as method as in javascript or VBA handling.
I get  an error message from the following php code   : Fatal error: Call to a member function
getElementByTagName()
on a non-object  on line 10 ,which is exact same line at $data= $dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)>nodeValue;
In my code, I try to access the remote-site with $c varaible to program the $url, there is 10 pages I need to
grab the data, if one of fail, I will re-do it until 5 times is fail.

I don't care the fatal error but it will halt the program running or suspend everything , Why I don't care
because I know it is remote-site server is  busy only  to handle my request of LoadHTMLFile().

Now I put try and catch {} to let it re-do again for 4 more times once fatal error happen, But
it still get the same line ans same fatal error message and quit the program right away, in other words,
it never go to catch{}.

Please advise, how to let the following code running even fatal-error because I can re-do 4 more time and then
quit the program. I know at least  one time success on LoadHTMlfile() work in 5 trial.

There is a lot of  Error handling at http://php.net/manual/en/function.set-error-handler.php,
I don't how it works becuase I know once the fatal-error happen, the program will quit so how to
do any if or condition check for the error happen location.


Duncan




<?php
For ($k=0; $k <4; ++$k)  {  //repeat loadhtmlfile() over again if one of $c is not loaded
try {
for ($c=0; $c < 10; ++$c) {
$url= 'http://www.othersite.com/ex.aspx?symbol='.$c;
$dom = new DOMDocument();
$dom->loadHTMLFile($url);
echo "start=".$c;
$data= $dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)>nodeValue;
echo "Sucess to pass getElementsByTagname".$c;
}
$k=4;//It means no need to do re-do since no fatal error during loadHTMlfile for 10 pages
}
catch(err) {
echo "It found error at =" %c;
}
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duncanb7
duncanb7

ASKER

Sorry, the $c  from 0 to 9, for example, $c=6 , is not valid that is I intended to try make fatal error to test
try and catch {} to see it works or not for try and catch{}, Actuall, there is no such page of $c=6 that I already
know it
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your reply, I change it is still fail because I intend to
go to the wbesite that is not existied for $c=6 in oder to test
the try and catch {} so I get fatal error is Normal and Accepted

But let the fatal error  and go to catch {] so that we can re-do it
until 5 times and quit the program .
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, you are correct, once fatal error happen, script will abort, whatever you is no use, Right ?
For example, if $tabitems = $dom->getElementsByTagName('table') will happen
fatal error, the other code like if condition checking is not running, Right,
So, in other words, the only way to solve the issue , it is "don't let have fatal Error Happen ", Right ?


$tabitems = $dom->getElementsByTagName('table');
if (! ($item = $tabitems->item(4)) instanceof DOMNode)) {
    throw new Exception();
}


So, in google seach, people are talking error handling or exception that is NOT fatal error, Right,
otherwise how to handle.?
So, from the article, I don't quit understand,

If it is fatal error, whatever you do the code or shutdown  funcation that is no use because
once fata error, php script abort, Right ?

I also pyt try{} in the loop, it still fail I think because of the same reason.
So it is related to Linux Adm system issue ? Should let users to catch the error ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Corect what I said, The code you provide  is help and the fatal error is gone, and
I believe loadHTMLfile() is no issue for non-exist webpage because it still has header even content is
empty that won't create fatal error.

If avoid, the fatal error and re-run,I will consider using  Linux c script to control php program like bat file
in window.
 
$data = "";
$d1 = $dom->getElementsByTagName('table');
if (!$d1)
{
  $d2 = $ d1->item(4);
  if (!$d2)
  {
     $d3 = $d2->getElementsByTagName('div')
     if (!$d4)
     {
        $d5 = $d4->item(21);
        if (!$d5) { $data = $d5->nodeValue; }
     }
   }
}
Dear angelIII,

Is (!$d1) or ($d1), since it is passed as i said  a minute ago because it never go into if condition.
and I put echo statement , it never echo so it pass fatal evero but output for $data is empty
! sign should check it is null variable
Please advise
Duncan

$d1 = $dom->getElementsByTagName('table');
if (!$d1)
{

echo "Testing*****************";
  $d2 = $ d1->item(4);
  if (!$d2)
  {
     $d3 = $d2->getElementsByTagName('div')
     if (!$d4)
     {
        $d5 = $d4->item(21);
        if (!$d5) { $data = $d5->nodeValue; }
     }
   }
} Accept and Award Points Accept as Solution
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all of your reply,
It might go to the new thread
for more fatal-error testing