Unable to exceute the code inside the catch block, when there is a file exception in laravel










2















I am unable to execute a code inside catch block when there is a file exception. Below is the code.



try 

// Check for file size. which will make sure file exists in local server.
filesize($localPath);
return 'success';
catch(FileException $e)

Log::error('Error reading file size ' . $e->getMessage());
$failedAttempts = $failedAttempts + 1;

// Set to sleep for 10.
sleep(10);

// Start recursive call.
$this->downloadMedia($url, $localPath, $failedAttempts);



I also tried Exception and ErrorException but nothing worked. Any help is appreciated.










share|improve this question


























    2















    I am unable to execute a code inside catch block when there is a file exception. Below is the code.



    try 

    // Check for file size. which will make sure file exists in local server.
    filesize($localPath);
    return 'success';
    catch(FileException $e)

    Log::error('Error reading file size ' . $e->getMessage());
    $failedAttempts = $failedAttempts + 1;

    // Set to sleep for 10.
    sleep(10);

    // Start recursive call.
    $this->downloadMedia($url, $localPath, $failedAttempts);



    I also tried Exception and ErrorException but nothing worked. Any help is appreciated.










    share|improve this question
























      2












      2








      2








      I am unable to execute a code inside catch block when there is a file exception. Below is the code.



      try 

      // Check for file size. which will make sure file exists in local server.
      filesize($localPath);
      return 'success';
      catch(FileException $e)

      Log::error('Error reading file size ' . $e->getMessage());
      $failedAttempts = $failedAttempts + 1;

      // Set to sleep for 10.
      sleep(10);

      // Start recursive call.
      $this->downloadMedia($url, $localPath, $failedAttempts);



      I also tried Exception and ErrorException but nothing worked. Any help is appreciated.










      share|improve this question














      I am unable to execute a code inside catch block when there is a file exception. Below is the code.



      try 

      // Check for file size. which will make sure file exists in local server.
      filesize($localPath);
      return 'success';
      catch(FileException $e)

      Log::error('Error reading file size ' . $e->getMessage());
      $failedAttempts = $failedAttempts + 1;

      // Set to sleep for 10.
      sleep(10);

      // Start recursive call.
      $this->downloadMedia($url, $localPath, $failedAttempts);



      I also tried Exception and ErrorException but nothing worked. Any help is appreciated.







      php laravel laravel-5.7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 20:55









      Juluri VinayJuluri Vinay

      339




      339






















          1 Answer
          1






          active

          oldest

          votes


















          0














          if You check this manual You'll see filesize method does not throw exception.




          Returns the size of the file in bytes, or FALSE



          (and generates an error of level E_WARNING) in case of an error.




          and since looks like You've not enabled error reporting or display_error directive - You don't see that E_WARNING



          You can just throw exception manually:



          try 

          // Check for file existence or throw exception.
          if (!is_file($localPath))
          throw new Exception($localPath.' does not exists');

          return 'success';

          catch(Exception $e)

          // here goes exception handling



          Extra advice (out of question's scope):



          There is no logic to check for file existence in recurring way.



          If file is downloaded it will exist otherwise You'll never download it during recursion.



          What if someone will pass link to file that does not exists?



          Better jus download it and stop with success or exception.






          share|improve this answer

























          • if there is no file, there is no point in trying to download it further, in the next round

            – Takamura
            Nov 16 '18 at 5:24











          • @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

            – num8er
            Nov 16 '18 at 5:54












          • num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

            – Takamura
            Nov 16 '18 at 5:59












          • @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

            – num8er
            Nov 16 '18 at 6:02











          • @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

            – num8er
            Nov 16 '18 at 6:15











          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%2f53327769%2funable-to-exceute-the-code-inside-the-catch-block-when-there-is-a-file-exceptio%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          if You check this manual You'll see filesize method does not throw exception.




          Returns the size of the file in bytes, or FALSE



          (and generates an error of level E_WARNING) in case of an error.




          and since looks like You've not enabled error reporting or display_error directive - You don't see that E_WARNING



          You can just throw exception manually:



          try 

          // Check for file existence or throw exception.
          if (!is_file($localPath))
          throw new Exception($localPath.' does not exists');

          return 'success';

          catch(Exception $e)

          // here goes exception handling



          Extra advice (out of question's scope):



          There is no logic to check for file existence in recurring way.



          If file is downloaded it will exist otherwise You'll never download it during recursion.



          What if someone will pass link to file that does not exists?



          Better jus download it and stop with success or exception.






          share|improve this answer

























          • if there is no file, there is no point in trying to download it further, in the next round

            – Takamura
            Nov 16 '18 at 5:24











          • @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

            – num8er
            Nov 16 '18 at 5:54












          • num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

            – Takamura
            Nov 16 '18 at 5:59












          • @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

            – num8er
            Nov 16 '18 at 6:02











          • @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

            – num8er
            Nov 16 '18 at 6:15
















          0














          if You check this manual You'll see filesize method does not throw exception.




          Returns the size of the file in bytes, or FALSE



          (and generates an error of level E_WARNING) in case of an error.




          and since looks like You've not enabled error reporting or display_error directive - You don't see that E_WARNING



          You can just throw exception manually:



          try 

          // Check for file existence or throw exception.
          if (!is_file($localPath))
          throw new Exception($localPath.' does not exists');

          return 'success';

          catch(Exception $e)

          // here goes exception handling



          Extra advice (out of question's scope):



          There is no logic to check for file existence in recurring way.



          If file is downloaded it will exist otherwise You'll never download it during recursion.



          What if someone will pass link to file that does not exists?



          Better jus download it and stop with success or exception.






          share|improve this answer

























          • if there is no file, there is no point in trying to download it further, in the next round

            – Takamura
            Nov 16 '18 at 5:24











          • @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

            – num8er
            Nov 16 '18 at 5:54












          • num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

            – Takamura
            Nov 16 '18 at 5:59












          • @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

            – num8er
            Nov 16 '18 at 6:02











          • @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

            – num8er
            Nov 16 '18 at 6:15














          0












          0








          0







          if You check this manual You'll see filesize method does not throw exception.




          Returns the size of the file in bytes, or FALSE



          (and generates an error of level E_WARNING) in case of an error.




          and since looks like You've not enabled error reporting or display_error directive - You don't see that E_WARNING



          You can just throw exception manually:



          try 

          // Check for file existence or throw exception.
          if (!is_file($localPath))
          throw new Exception($localPath.' does not exists');

          return 'success';

          catch(Exception $e)

          // here goes exception handling



          Extra advice (out of question's scope):



          There is no logic to check for file existence in recurring way.



          If file is downloaded it will exist otherwise You'll never download it during recursion.



          What if someone will pass link to file that does not exists?



          Better jus download it and stop with success or exception.






          share|improve this answer















          if You check this manual You'll see filesize method does not throw exception.




          Returns the size of the file in bytes, or FALSE



          (and generates an error of level E_WARNING) in case of an error.




          and since looks like You've not enabled error reporting or display_error directive - You don't see that E_WARNING



          You can just throw exception manually:



          try 

          // Check for file existence or throw exception.
          if (!is_file($localPath))
          throw new Exception($localPath.' does not exists');

          return 'success';

          catch(Exception $e)

          // here goes exception handling



          Extra advice (out of question's scope):



          There is no logic to check for file existence in recurring way.



          If file is downloaded it will exist otherwise You'll never download it during recursion.



          What if someone will pass link to file that does not exists?



          Better jus download it and stop with success or exception.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 6:10

























          answered Nov 15 '18 at 21:05









          num8ernum8er

          11.9k22040




          11.9k22040












          • if there is no file, there is no point in trying to download it further, in the next round

            – Takamura
            Nov 16 '18 at 5:24











          • @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

            – num8er
            Nov 16 '18 at 5:54












          • num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

            – Takamura
            Nov 16 '18 at 5:59












          • @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

            – num8er
            Nov 16 '18 at 6:02











          • @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

            – num8er
            Nov 16 '18 at 6:15


















          • if there is no file, there is no point in trying to download it further, in the next round

            – Takamura
            Nov 16 '18 at 5:24











          • @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

            – num8er
            Nov 16 '18 at 5:54












          • num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

            – Takamura
            Nov 16 '18 at 5:59












          • @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

            – num8er
            Nov 16 '18 at 6:02











          • @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

            – num8er
            Nov 16 '18 at 6:15

















          if there is no file, there is no point in trying to download it further, in the next round

          – Takamura
          Nov 16 '18 at 5:24





          if there is no file, there is no point in trying to download it further, in the next round

          – Takamura
          Nov 16 '18 at 5:24













          @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

          – num8er
          Nov 16 '18 at 5:54






          @Takamura it's the same function, Juluri tries to check for file existence again "fancy way" using recursion (: about my answer - his question is about why function does not throw error in try..catch. so I've explained why. but in fact that way of doing download is incorrect. So I agree with You.

          – num8er
          Nov 16 '18 at 5:54














          num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

          – Takamura
          Nov 16 '18 at 5:59






          num8er if the file is missing for a long time, a memory overflow may occur, is it better to use an iterator? )

          – Takamura
          Nov 16 '18 at 5:59














          @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

          – num8er
          Nov 16 '18 at 6:02





          @Takamura man, I'm answering by context of question. I don't care if someone wrote overflowing code

          – num8er
          Nov 16 '18 at 6:02













          @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

          – num8er
          Nov 16 '18 at 6:15






          @Takamura btw php will not have memory overflow for that block of code since it's just iterating integer variable. It will stop execute in terms of recursion_limit in config of PHP. php.net/manual/en/pcre.configuration.php and for 1000 recursion limit and 10 seconds sleep it will take ~6 days which someone (admins, hosting providers) will get informed that something goes wrong.

          – num8er
          Nov 16 '18 at 6:15




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327769%2funable-to-exceute-the-code-inside-the-catch-block-when-there-is-a-file-exceptio%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党