Ask Question Forum:
Model Library:2025-02-08:A.I. model is online auto reply
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-04-27 05:43:46
Point:500 Replies:13 POST_ID:828490USER_ID:11059
Topic:
PHP Scripting Language;JavaScript;Visual Basic Programming
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
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
<?phpFor ($k=0; $k <4; ++$k) { //repeat loadhtmlfile() over again if one of $c is not loadedtry {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;}}?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:
Author: duncanb7 replied at 2024-04-27 09:08:35
Thanks for all of your reply,
It might go to the new thread
for more fatal-error testing
It might go to the new thread
for more fatal-error testing
Assisted Solution
Expert: Guy Hengel [angelIII / a3] replied at 2024-04-27 07:53:38
84 points EXCELLENT
sorry... my fault. it must be if ($d1) and not if (!$d1) ... same for the other tests.
Author: duncanb7 replied at 2024-04-27 07:49:56
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
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
Author: duncanb7 replied at 2024-04-27 07:29:35
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; }
}
}
}
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; }
}
}
}
Assisted Solution
Expert: Ray Paseur replied at 2024-04-27 06:23:42
83 points EXCELLENT
This is pretty unambiguous: Call to a member function getElementByTagName() on a non-object on line 10.
You would want to know what created the data you're using on line 10. You can test to see if the data is an object with this function:
http://us3.php.net/manual/en/function.is-object.php
Probably, either something failed to create the object (and returned FALSE) in an earlier instruction, or the data you've got does not have an object at the index level you've addressed. In either case, the right thing to do is program so that no fatal error occurs, and that means testing the data for the correct type before using it.
Here is a microcosm example of how the error happens. Note that this is different for non-arrays and non-objects.
You would want to know what created the data you're using on line 10. You can test to see if the data is an object with this function:
http://us3.php.net/manual/en/function.is-object.php
Probably, either something failed to create the object (and returned FALSE) in an earlier instruction, or the data you've got does not have an object at the index level you've addressed. In either case, the right thing to do is program so that no fatal error occurs, and that means testing the data for the correct type before using it.
Here is a microcosm example of how the error happens. Note that this is different for non-arrays and non-objects.
<?php // RAY_expected_error.phperror_reporting(E_ALL);// CREATE A NON-OBJECT (THIS IS A STRING)$foo = 'bar';// WARNING IF WE TRY TO USE WITH AN ITERATORforeach ($foo as $lump){ /* STUFF */}// FATAL ERROR IF WE TRY TO USE A NON-OBJECT WITH OOP NOTATION$thing = $foo->baz(); 1:2:3:4:5:6:7:8:9:10:11:12:13:
Author: duncanb7 replied at 2024-04-27 06:21:18
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 ?
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 ?
Author: duncanb7 replied at 2024-04-27 06:13:58
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.?
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.?
Assisted Solution
Expert: Guy Hengel [angelIII / a3] replied at 2024-04-27 06:09:46
83 points EXCELLENT
catch fatal errors:
http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error
http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error
Author: duncanb7 replied at 2024-04-27 06:07:03
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 .
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 .
Assisted Solution
Expert: hernst42 replied at 2024-04-27 06:03:42
83 points EXCELLENT
Fatal errors in php can not be catched as they are fatal and cause an immediate aboirt of the php script.
The dom extension itself is not designed around the new features of php 5 like exceptions. So it would be cool if the dom extension would throw an exception if an element does not exists instead of reaturn a null value. So you need to code this by yourself:
The dom extension itself is not designed around the new features of php 5 like exceptions. So it would be cool if the dom extension would throw an exception if an element does not exists instead of reaturn a null value. So you need to code this by yourself:
$tabitems = $dom->getElementsByTagName('table');if (! ($item = $tabitems->item(4)) instanceof DOMNode)) { throw new Exception();}$divitems = $itme->getElementsByTagName('div')if (! ($item = $divitems->item(21)) instanceof DOMNode)) { throw new Exception();}return $item->nodeValue; 1:2:3:4:5:6:7:8:9:
Assisted Solution
Expert: Guy Hengel [angelIII / a3] replied at 2024-04-27 06:02:12
83 points EXCELLENT
i see,
please put the try{} inside the loop
please put the try{} inside the loop
Author: duncanb7 replied at 2024-04-27 05:53:31
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
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
Accepted Solution
Expert: Guy Hengel [angelIII / a3] replied at 2024-04-27 05:49:57
84 points EXCELLENT
Fatal error: Call to a member function getElementByTagName() on a non-object
the part in bold is the important part. at least at some level of the call, there is a non-object ...
either of these is returning a non-object (aka empty or null):
$dom
$dom->getElementsByTagName('table')
$dom->getElementsByTagName('table')->item(4)
$dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')
$dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)
so, you have 2 options:
get every level apart, and check every level, or "ignore" errors
I would go like this:
the part in bold is the important part. at least at some level of the call, there is a non-object ...
either of these is returning a non-object (aka empty or null):
$dom
$dom->getElementsByTagName('table')
$dom->getElementsByTagName('table')->item(4)
$dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')
$dom->getElementsByTagName('table')->item(4)->getElementsByTagName('div')->item(21)
so, you have 2 options:
get every level apart, and check every level, or "ignore" errors
I would go like this:
$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; } } }} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15: