How to continue a failed loop php











up vote
0
down vote

favorite












how do you continue a loop that an iteration has failed but still want the remaining part of the loop to be run.



$arr = [1, 2, 3, 4];

foreach($arr as $val) {
if($val == 2) {
throw new Exception('Value = 2');
}

try {
echo $val;
}
catch(Execption $e) {
echo $e;
}
}


How do i go about this










share|improve this question
























  • You're trying to catch an exception thrown outside your try block. That's not how exceptions work.
    – Jeto
    Nov 20 at 4:47








  • 1




    $arr = [1,2,3,4]; foreach($arr as $val){ if($val == 2) continue; echo $val; }
    – sh4rifi
    Nov 20 at 4:50










  • Why do you throw an exception if you want your code to continue running?
    – Nico Haase
    Nov 20 at 8:29










  • i am not actually throwing the exception manually, i am just trying to recreate a type of situation am in my code, i am sending emails instead, of exceptions, but the emails return error from time to time and i want to be able to handle this.
    – Maxwell
    Nov 20 at 8:34















up vote
0
down vote

favorite












how do you continue a loop that an iteration has failed but still want the remaining part of the loop to be run.



$arr = [1, 2, 3, 4];

foreach($arr as $val) {
if($val == 2) {
throw new Exception('Value = 2');
}

try {
echo $val;
}
catch(Execption $e) {
echo $e;
}
}


How do i go about this










share|improve this question
























  • You're trying to catch an exception thrown outside your try block. That's not how exceptions work.
    – Jeto
    Nov 20 at 4:47








  • 1




    $arr = [1,2,3,4]; foreach($arr as $val){ if($val == 2) continue; echo $val; }
    – sh4rifi
    Nov 20 at 4:50










  • Why do you throw an exception if you want your code to continue running?
    – Nico Haase
    Nov 20 at 8:29










  • i am not actually throwing the exception manually, i am just trying to recreate a type of situation am in my code, i am sending emails instead, of exceptions, but the emails return error from time to time and i want to be able to handle this.
    – Maxwell
    Nov 20 at 8:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











how do you continue a loop that an iteration has failed but still want the remaining part of the loop to be run.



$arr = [1, 2, 3, 4];

foreach($arr as $val) {
if($val == 2) {
throw new Exception('Value = 2');
}

try {
echo $val;
}
catch(Execption $e) {
echo $e;
}
}


How do i go about this










share|improve this question















how do you continue a loop that an iteration has failed but still want the remaining part of the loop to be run.



$arr = [1, 2, 3, 4];

foreach($arr as $val) {
if($val == 2) {
throw new Exception('Value = 2');
}

try {
echo $val;
}
catch(Execption $e) {
echo $e;
}
}


How do i go about this







php laravel loops foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:28

























asked Nov 20 at 4:40









Maxwell

319




319












  • You're trying to catch an exception thrown outside your try block. That's not how exceptions work.
    – Jeto
    Nov 20 at 4:47








  • 1




    $arr = [1,2,3,4]; foreach($arr as $val){ if($val == 2) continue; echo $val; }
    – sh4rifi
    Nov 20 at 4:50










  • Why do you throw an exception if you want your code to continue running?
    – Nico Haase
    Nov 20 at 8:29










  • i am not actually throwing the exception manually, i am just trying to recreate a type of situation am in my code, i am sending emails instead, of exceptions, but the emails return error from time to time and i want to be able to handle this.
    – Maxwell
    Nov 20 at 8:34


















  • You're trying to catch an exception thrown outside your try block. That's not how exceptions work.
    – Jeto
    Nov 20 at 4:47








  • 1




    $arr = [1,2,3,4]; foreach($arr as $val){ if($val == 2) continue; echo $val; }
    – sh4rifi
    Nov 20 at 4:50










  • Why do you throw an exception if you want your code to continue running?
    – Nico Haase
    Nov 20 at 8:29










  • i am not actually throwing the exception manually, i am just trying to recreate a type of situation am in my code, i am sending emails instead, of exceptions, but the emails return error from time to time and i want to be able to handle this.
    – Maxwell
    Nov 20 at 8:34
















You're trying to catch an exception thrown outside your try block. That's not how exceptions work.
– Jeto
Nov 20 at 4:47






You're trying to catch an exception thrown outside your try block. That's not how exceptions work.
– Jeto
Nov 20 at 4:47






1




1




$arr = [1,2,3,4]; foreach($arr as $val){ if($val == 2) continue; echo $val; }
– sh4rifi
Nov 20 at 4:50




$arr = [1,2,3,4]; foreach($arr as $val){ if($val == 2) continue; echo $val; }
– sh4rifi
Nov 20 at 4:50












Why do you throw an exception if you want your code to continue running?
– Nico Haase
Nov 20 at 8:29




Why do you throw an exception if you want your code to continue running?
– Nico Haase
Nov 20 at 8:29












i am not actually throwing the exception manually, i am just trying to recreate a type of situation am in my code, i am sending emails instead, of exceptions, but the emails return error from time to time and i want to be able to handle this.
– Maxwell
Nov 20 at 8:34




i am not actually throwing the exception manually, i am just trying to recreate a type of situation am in my code, i am sending emails instead, of exceptions, but the emails return error from time to time and i want to be able to handle this.
– Maxwell
Nov 20 at 8:34












2 Answers
2






active

oldest

votes

















up vote
1
down vote













You can try something like this pattern if want to follow the pattern.



$arr = [1, 2, 3, 4];

foreach($arr as $a) {
try {
if ( $a == 2 ) {
throw new Exception('Exception');
}

echo $a.' ';
}
catch(Exception $e) {
echo 'Message ';
}

echo PHP_EOL;
}


it will help for not breaking the loop



Result will be as:



1 Message 3 4


Hope it helps.






share|improve this answer






























    up vote
    0
    down vote













    Rather than throw an exception you can create a Log



    Log::error('Array value = ' . $val);
    continue;


    This will record the issue but still allow the processing of the rest of the array



    There is more details here:



    https://laravel.com/docs/5.7/logging






    share|improve this answer























    • i am not actually throwing the exception manually, i am just trying to create a type of situation am in
      – Maxwell
      Nov 20 at 8:16










    • If you can give us more details about your situation then we might be able to help you more
      – Josh
      Nov 21 at 2:13











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386355%2fhow-to-continue-a-failed-loop-php%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You can try something like this pattern if want to follow the pattern.



    $arr = [1, 2, 3, 4];

    foreach($arr as $a) {
    try {
    if ( $a == 2 ) {
    throw new Exception('Exception');
    }

    echo $a.' ';
    }
    catch(Exception $e) {
    echo 'Message ';
    }

    echo PHP_EOL;
    }


    it will help for not breaking the loop



    Result will be as:



    1 Message 3 4


    Hope it helps.






    share|improve this answer



























      up vote
      1
      down vote













      You can try something like this pattern if want to follow the pattern.



      $arr = [1, 2, 3, 4];

      foreach($arr as $a) {
      try {
      if ( $a == 2 ) {
      throw new Exception('Exception');
      }

      echo $a.' ';
      }
      catch(Exception $e) {
      echo 'Message ';
      }

      echo PHP_EOL;
      }


      it will help for not breaking the loop



      Result will be as:



      1 Message 3 4


      Hope it helps.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        You can try something like this pattern if want to follow the pattern.



        $arr = [1, 2, 3, 4];

        foreach($arr as $a) {
        try {
        if ( $a == 2 ) {
        throw new Exception('Exception');
        }

        echo $a.' ';
        }
        catch(Exception $e) {
        echo 'Message ';
        }

        echo PHP_EOL;
        }


        it will help for not breaking the loop



        Result will be as:



        1 Message 3 4


        Hope it helps.






        share|improve this answer














        You can try something like this pattern if want to follow the pattern.



        $arr = [1, 2, 3, 4];

        foreach($arr as $a) {
        try {
        if ( $a == 2 ) {
        throw new Exception('Exception');
        }

        echo $a.' ';
        }
        catch(Exception $e) {
        echo 'Message ';
        }

        echo PHP_EOL;
        }


        it will help for not breaking the loop



        Result will be as:



        1 Message 3 4


        Hope it helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 6:12









        DestinatioN

        1,2501326




        1,2501326










        answered Nov 20 at 5:58









        kshitij

        18210




        18210
























            up vote
            0
            down vote













            Rather than throw an exception you can create a Log



            Log::error('Array value = ' . $val);
            continue;


            This will record the issue but still allow the processing of the rest of the array



            There is more details here:



            https://laravel.com/docs/5.7/logging






            share|improve this answer























            • i am not actually throwing the exception manually, i am just trying to create a type of situation am in
              – Maxwell
              Nov 20 at 8:16










            • If you can give us more details about your situation then we might be able to help you more
              – Josh
              Nov 21 at 2:13















            up vote
            0
            down vote













            Rather than throw an exception you can create a Log



            Log::error('Array value = ' . $val);
            continue;


            This will record the issue but still allow the processing of the rest of the array



            There is more details here:



            https://laravel.com/docs/5.7/logging






            share|improve this answer























            • i am not actually throwing the exception manually, i am just trying to create a type of situation am in
              – Maxwell
              Nov 20 at 8:16










            • If you can give us more details about your situation then we might be able to help you more
              – Josh
              Nov 21 at 2:13













            up vote
            0
            down vote










            up vote
            0
            down vote









            Rather than throw an exception you can create a Log



            Log::error('Array value = ' . $val);
            continue;


            This will record the issue but still allow the processing of the rest of the array



            There is more details here:



            https://laravel.com/docs/5.7/logging






            share|improve this answer














            Rather than throw an exception you can create a Log



            Log::error('Array value = ' . $val);
            continue;


            This will record the issue but still allow the processing of the rest of the array



            There is more details here:



            https://laravel.com/docs/5.7/logging







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 at 5:40

























            answered Nov 20 at 5:33









            Josh

            590111




            590111












            • i am not actually throwing the exception manually, i am just trying to create a type of situation am in
              – Maxwell
              Nov 20 at 8:16










            • If you can give us more details about your situation then we might be able to help you more
              – Josh
              Nov 21 at 2:13


















            • i am not actually throwing the exception manually, i am just trying to create a type of situation am in
              – Maxwell
              Nov 20 at 8:16










            • If you can give us more details about your situation then we might be able to help you more
              – Josh
              Nov 21 at 2:13
















            i am not actually throwing the exception manually, i am just trying to create a type of situation am in
            – Maxwell
            Nov 20 at 8:16




            i am not actually throwing the exception manually, i am just trying to create a type of situation am in
            – Maxwell
            Nov 20 at 8:16












            If you can give us more details about your situation then we might be able to help you more
            – Josh
            Nov 21 at 2:13




            If you can give us more details about your situation then we might be able to help you more
            – Josh
            Nov 21 at 2:13


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386355%2fhow-to-continue-a-failed-loop-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin