Catch the fatal exception and continue

I know that by its very definition, a fatal exception should kill execution and should not be suppressed, but this is the problem.

I run a script that flushes, parses and stores about 10,000 pages in the database. This takes a couple of hours, and in rare cases (1 out of 1000) the page does not parse and throws a fatal exception.

I am currently 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 does not work for fatal exceptions .

Can I do something like this: If I move the main loop to 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 appears when execution stops:

 Fatal error: Call to a member function find() on a non-object in ... 

I expect this above when the page is not parsed by the method I use. I'm fine just skipping this page and moving on to the next iteration of the loop.

+6
source share
4 answers

Fatal errors are fatal and terminate. There is no way around this if a fatal error occurs. However, your mistake:

Fatal error: call of find () member function for non-object in ...

completely prevented. Just add a check to make sure you have an instance of the correct object, and if not, handle the error:

 if ($foo instanceof SomeObject) { $foo->find(...); } else { // something went wrong } 
+10
source

First, there is a clear distinction between exceptions and errors . What you encounter is a mistake, not an exception. Based on your post and the code you posted, the problem is that you didn't ask in your question. What variable are you trying to call find() ? Well, this variable is not an object. It is impossible to catch fatal errors and ignore them, you must find where you call find() on the non-object and fix it.

+6
source

It seems to me that the only possible way to "catch" a false error is to register a shutdown function. Remember to add all (or possibly groups) of requests to the transaction and possibly drop them back if something fails, just to ensure consistency.

+1
source

I had a similar problem with this, and I found that using the is_object () call before the find () call avoids a fatal error.

+1
source

Source: https://habr.com/ru/post/905844/


All Articles