How can I handle an exception happening in a foreach loop?
I want to throw my exception if the for loop didn’t work properly.
As data is huge, foreach exits because PHP’s memory limit is exceeded.
try
{
foreach()
}catch (exception $e)
{
echo $e;
}
This is not working. How do I throw an exception?
Memory exceeded is a fatal error, not an exception and cannot be handled with try/catch blocks.
What you need is set_error_handler.
EDIT: If that does not work you can use register_shutdown_function as a last resort and check if the script was stopped by and error.