Note that there are some explanatory texts on larger screens.

plurals
  1. POCatch a fatal exception and continue
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Currently, I'm doing this:</p> <pre><code>for ($i=0;$i&lt;$count;$i++) { $classObject = $classObjects[$i]; echo $i . " : " . memory_get_usage(true) . "\n"; $classDOM = $scraper-&gt;scrapeClassInfo($classObject,$termMap,$subjectMap); $class = $parser-&gt;parseClassInfo($classDOM); $dbmanager-&gt;storeClassInfo($class); unset($classDOM,$class,$classObject); } </code></pre> <p>Can I do something like</p> <pre><code>for ($i=0;$i&lt;$count;$i++) { $classObject = $classObjects[$i]; echo $i . " : " . memory_get_usage(true) . "\n"; try { $classDOM = $scraper-&gt;scrapeClassInfo($classObject,$termMap,$subjectMap); $class = $parser-&gt;parseClassInfo($classDOM); $dbmanager-&gt;storeClassInfo($class); unset($classDOM,$class,$classObject); } catch (Exception $e) { //log the error here continue; } } </code></pre> <p>The code above doesn't work for <code>fatal exceptions</code>.</p> <p>Would it be possible to do something like this: If I moved the main loop into a method, and then call the method from <code>register_shutdown_function</code> ?</p> <p>Like this:</p> <pre><code>function do($start) { for($i=$start;$i&lt;$count;$i++) { //do stuff here } } register_shutdown_function('shutdown'); function shutdown() { do(); } </code></pre> <p>This is the message that is output when execution stops:</p> <pre><code>Fatal error: Call to a member function find() on a non-object in ... </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload