0

I've read this thread: php: catch exception and continue execution, is it possible?

Every answer suggests that a try catch will continue executing the script. Here is an example where it doesn't:

  try{ $load = @sys_getloadavg(); }
  catch (Exception $e){ echo 'Couldn\'t find load average.<br>'; return false; }

I'm running it on xampp on windows, which could be why it errors (it gives a Call to undefined function sys_getloadavg() error when the @ is removed), but that isn't the issue in question. It could be any function that doesn't exist, isn't supported or fails - I can not get the script to continue executing.

Another example is if there is a syntax error in the try, say I'm including an external file and parsing it as an array. This also produces an error and stops executing.

Is there any brute force way to continue the script running, regardless of what fails in the try?

Community
  • 1
  • 1
John
  • 11,985
  • 3
  • 45
  • 60
  • Sidenote to save a keystroke `echo "Couldn't find load average.
    ";` ;-)
    – Funk Forty Niner Mar 07 '14 at 16:58
  • What /is/ happening when you run this? – VBCPP Mar 07 '14 at 17:02
  • It stop executing the rest of the script. Could be anything bad in the `try{}` though. I could have a whole page of code in the `try{}`, if it errors, it will stop execution instead of bouncing to the catch exception @VBCPP – John Mar 07 '14 at 17:04

1 Answers1

4

Unlike other languages, there's a difference in PHP between exceptions and errors. This would be like a compile error in other languages. that require declaration files. You can't catch or ignore Fatal errors like a function not exisiting. You can test for existence before using though:

 if( function_exists('sys_getloadavg') {
       try{ $load = @sys_getloadavg(); }
           catch (Exception $e){ echo 'Couldn\'t find load average.<br>'; return false; }
 }
Ray
  • 40,256
  • 21
  • 101
  • 138