0

I have this kind of loop. On each iteration it should include a file, Included files can come with errors. Once some of included files gets an error the whole process of getting lost. How to prevent breaking of the process? I tried this try catch but it errors from included files still cause stopping execution of file. Thanks

foreach ($li_arrays as $index => $li_array) {
    if($index == 0){

        try {
            require 'update.php';
        } catch(Exception $e) {
            echo "Exception caught with message: " . $e->getMessage() . "\n";
        }

    }
    elseif ($index == 1){
        try {
            require 'update1.php';
        } catch(Exception $e) {
            echo "Exception caught with message: " . $e->getMessage() . "\n";
        }
    }else{
        try {
            require 'update2.php';
        } catch(Exception $e) {
            echo "Exception caught with message: " . $e->getMessage() . "\n";
        }
    }
4
  • 2
    Simply use include instead of require Commented Dec 17, 2018 at 16:42
  • 2
    What do you mean with "Included files can come with errors"? If those files contain syntax errors, then fix those errors ! Commented Dec 17, 2018 at 16:43
  • I hope you do not attempt to implement a setup where you execute php files you have no control over? Like files uploaded by clients? That would be a mile wide security gap ... Anyone could do anything with your system. Commented Dec 17, 2018 at 16:44
  • Makes sense. Thanks. Commented Dec 17, 2018 at 16:57

2 Answers 2

1

Errors in php are not recoverable, so they will always lead to the termination of your script.

I am not even sure that you are even talking about Errors, though if you are, this is the answer to your question.

Another thing to be aware of: Require will throw an E_COMPILE_ERROR if the required file doesn't exist, which is also something you won't be able to catch. If you don't want to terminate if the script isn't found, use include instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I'll replace "require" with "include". I'll try to catch exceptions inside those files.
@Geobo depending on the context of what you are trying to do, there might be another possible alternative to include: php.net/manual/en/ref.exec.php (executing an external script) If the external script terminates because of an error, this doesn't automatically lead to a termination of the calling script.
0

At the end I changed "require" with "include" and used this try-catch approach inside each included file.

$attempts = 0;

    do {
        try
        {
           ////PHP code ///
        } catch (Exception $e) {
            echo "\n\n======EXCEPTION======\n\n";
            var_dump($e);
            $attempts++;
            sleep(30);
            continue;
        }
        break;
    } while($attempts < 5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.