I know, that by its very definition, a fatal exception is supposed to kill the execution, and should not be suppressed, but here's the issue.
I'm running a script that scrapes, parses and stores in a DB about 10,000 pages. This takes a couple of hours, and in rare cases (1 in 1000) a page fails parsing and throws a fatal exception.
Currently, I'm doing this:
for ($i=0;$i<$count;$i++)
{
$classObject = $classObjects[$i];
echo $i . " : " . memory_get_usage(true) . "\n";
$classDOM = $scraper->scrapeClassInfo($classObject,$termMap,$subjectMap);
$class = $parser->parseClassInfo($classDOM);
$dbmanager->storeClassInfo($class);
unset($classDOM,$class,$classObject);
}
Can I do something like
for ($i=0;$i<$count;$i++)
{
$classObject = $classObjects[$i];
echo $i . " : " . memory_get_usage(true) . "\n";
try
{
$classDOM = $scraper->scrapeClassInfo($classObject,$termMap,$subjectMap);
$class = $parser->parseClassInfo($classDOM);
$dbmanager->storeClassInfo($class);
unset($classDOM,$class,$classObject);
}
catch (Exception $e)
{
//log the error here
continue;
}
}
The code above doesn't work for fatal exceptions
.
Would it be possible to do something like this:
If I moved the main loop into a method, and then call the method from register_shutdown_function
?
Like this:
function do($start)
{
for($i=$start;$i<$count;$i++)
{
//do stuff here
}
}
register_shutdown_function('shutdown');
function shutdown()
{
do();
}
This is the message that is output when execution stops:
Fatal error: Call to a member function find() on a non-object in ...
I expect this above message when a page isn't parse-able by the method I am using. I'm fine with just skipping that page and moving on to the next iteration of the loop.
fatal exception
? How is yourcatch (Exception $e)
not working?Exception
?} catch ($e) {
?Fatal error
. What is the Fatal error message you recieve? This will give you a clue as to what needs to be done. May not be an exception at all.fatal exception
but afatal error
.