We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Fatal Error make my php quit right away

Medium Priority
457 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2007
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
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 .
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
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.?

Author

Commented:
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 ?
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
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; }
     }
   }
}

Author

Commented:
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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks for all of your reply,
It might go to the new thread
for more fatal-error testing
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.