Answers.Body: <p>The exception is thrown before you enter your catch block during this line</p>
  
  <pre><code> $number = $twilio-&gt;lookups-&gt;v1-&gt;phoneNumbers($user-&gt;mobil)-&gt;fetch(['countryCode' =&gt; 'NO'])-&gt;phoneNumber; 
  </code></pre>
  
  <p>That's why it can't be cached. Move it into the same block and handle every single possible exception that can be thrown accordingly or via generic <code>\Exception</code> class </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-24T03:11:10.923Z
Answers.LastActivityDate: 2019-02-24T03:11:10.923Z
Answers.UserDisplayName: Robin Schambach
Body: <p>Not sure if this is the best place to post, but since it's a Craft 3 plugin I'll give it a shot.</p>
  
  <pre><code>foreach ($numbers as $number){
      $number = $twilio-&gt;lookups-&gt;v1-&gt;phoneNumbers($user-&gt;mobil)-&gt;fetch(['countryCode' =&gt; 'NO'])-&gt;phoneNumber;
  
      try {
          // Send SMS
          $twilio-&gt;messages-&gt;create(
              $number,
              [
                  'from' =&gt; $settings-&gt;twilioNumber,
                  'body' =&gt; $request-&gt;post('body')
              ]
          );
      } catch (\Exception $e) {
          continue; // Continue (loop) to next number as this one failed/is invalid.
      }
  }
  </code></pre>
  
  <p>In short, whenever a <code>$number</code> is invalid, execution stops giving <code>Twilio\Exceptions\RestException [HTTP 404] Unable to fetch record: The requested resource /PhoneNumbers/"the-invalid-number" was not found</code>.</p>
  
  <p>It seems that the <code>catch</code> is never reached.</p>
  
  <p>Note that I have tried replacing <code>\Exception</code> with the following to no avail.</p>
  
  <pre><code>use Twilio\Exceptions\ConfigurationException;
  use Twilio\Exceptions\DeserializeException;
  use Twilio\Exceptions\EnvironmentException;
  use Twilio\Exceptions\HttpException;
  use Twilio\Exceptions\RestException;
  use Twilio\Exceptions\TwimlException;
  </code></pre>
  
  <p>Regardless, I thought <code>\Exception</code> would catch any exception?</p>
  
  <p>Edit: Here's the solution.</p>
  
  <pre><code>try {
      // Validate the number
      $number = $twilio-&gt;lookups-&gt;v1-&gt;phoneNumbers($user-&gt;mobil)-&gt;fetch(['countryCode' =&gt; 'NO'])-&gt;phoneNumber;
  
      // Send SMS
      $twilio-&gt;messages-&gt;create(
          $number,
          [
              'from' =&gt; $settings-&gt;twilioNumber,
              'body' =&gt; $request-&gt;post('body')
          ]
      );
  } catch (\Exception $e) {
      // Number is invalid; continue to next number
      continue;
  }
  </code></pre>
  
Tags: <plugin-development>
Title: Not able to catch exception sending SMS with Twilio in plugin
ThreadText: # Not able to catch exception sending SMS with Twilio in plugin
  
  (Asked by: username_0 on 2019-02-23)
  
  Not sure if this is the best place to post, but since it's a Craft 3 plugin I'll give it a shot.
  
  ```
  foreach ($numbers as $number){
      $number = $twilio->lookups->v1->phoneNumbers($user->mobil)->fetch(['countryCode' => 'NO'])->phoneNumber;
  
      try {
          // Send SMS
          $twilio->messages->create(
              $number,
              [
                  'from' => $settings->twilioNumber,
                  'body' => $request->post('body')
              ]
          );
      } catch (\Exception $e) {
          continue; // Continue (loop) to next number as this one failed/is invalid.
      }
  }
  
  ```
  
  In short, whenever a `$number` is invalid, execution stops giving `Twilio\Exceptions\RestException [HTTP 404] Unable to fetch record: The requested resource /PhoneNumbers/"the-invalid-number" was not found`.
  
  It seems that the `catch` is never reached.
  
  Note that I have tried replacing `\Exception` with the following to no avail.
  
  ```
  use Twilio\Exceptions\ConfigurationException;
  use Twilio\Exceptions\DeserializeException;
  use Twilio\Exceptions\EnvironmentException;
  use Twilio\Exceptions\HttpException;
  use Twilio\Exceptions\RestException;
  use Twilio\Exceptions\TwimlException;
  
  ```
  
  Regardless, I thought `\Exception` would catch any exception?
  
  Edit: Here's the solution.
  
  ```
  try {
      // Validate the number
      $number = $twilio->lookups->v1->phoneNumbers($user->mobil)->fetch(['countryCode' => 'NO'])->phoneNumber;
  
      // Send SMS
      $twilio->messages->create(
          $number,
          [
              'from' => $settings->twilioNumber,
              'body' => $request->post('body')
          ]
      );
  } catch (\Exception $e) {
      // Number is invalid; continue to next number
      continue;
  }
  
  ```
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-24. Score: 2 | Accepted answer
  
  The exception is thrown before you enter your catch block during this line
  
  ```
   $number = $twilio->lookups->v1->phoneNumbers($user->mobil)->fetch(['countryCode' => 'NO'])->phoneNumber; 
  
  ```
  
  That's why it can't be cached. Move it into the same block and handle every single possible exception that can be thrown accordingly or via generic `\Exception` class
  
  
  ---
  
  