How do I handle exceptions in a service class? I have implemented a Service which does stuff with a RESTful API:
services:
  api.clientname:
    class: Drupal\my_custom_module\Api\Clientname
    arguments: ['@http_client', '@messenger', '@logger.factory', '@uuid']

I'm injecting dependencies for ['@http_client', '@messenger', '@logger.factory', '@uuid']:

Guzzle HTTP Client
messenger service to call $this-&gt;messenger-&gt;addMessage(),
logger factory to call $this-&gt;loggerFactory-&gt;get('my_custom_module')-&gt;debug($message, $variables),
UUID service to generate UUID v4, per API specification.

I'm also using the StringTranslationTrait to get $this-&gt;t() working in my class.
All this is great so far. When it works, it works, and it logs success to the dblog. But it also needs to log failures. I'm having a problem with exception handling.
I'm trying to do something like this:
    while ($apiCallAttempts &lt; API_CALL_LIMIT &amp;&amp; $apiSuccess === FALSE) {
      $apiCallAttempts++;
      try {
        $accessToken = $this-&gt;obtainAccessToken($url, $postFields);
        $message = &quot;Step 1: Access Token obtained from API endpoint&quot;;
        $this-&gt;loggerFactory-&gt;get('my_custom_module')-&gt;debug($message, []);
        $apiSuccess = TRUE;
      }
      catch (Exception $e) {
        // TODO: &quot;watchdog_exception('my_custom_module', $e);&quot;.
        $variables = Error::decodeException($e);
        $this-&gt;loggerFactory-&gt;get('my_custom_module')-&gt;error('%type: @message in %function (line %line of %file).', $variables);
        if ($apiCallAttempts === API_CALL_LIMIT &amp;&amp; $apiSuccess === FALSE) {
          // Set errorMessage for messenger service.
          $errorMessage = 'Error CLIENTNAME-01: Authentication API call failed';
          $message = 'Failed to sync %label with API. &lt;br&gt; @error after @limit attempts.';
          $variables = [
            '%label' =&gt; $form_id,
            '@error' =&gt; $errorMessage,
            '@limit' =&gt; strval($apiCallAttempts),
          ];
          $this-&gt;messenger-&gt;addMessage($errorMessage, 'error');
          $this-&gt;loggerFactory-&gt;get('dex_provider_hcp_forms')-&gt;error($message, $variables);
          return $accessToken;
        }
      }

but it doesn't work because:

Fatal error: Trait 'Drupal\my_custom_module\Api\Exception' not found in /app/docroot/modules/custom/my_custom_module/src/Api/Clientname.php on line 66

After some Googling, I am very confused about how to add the global PHP Exception to my Service class namespace.
Removing a use declaration inside the class removed the &quot;Trait not found&quot; error.
The new error message was:

Error: Class 'Drupal\my_custom_module\Api\Error' not found

EDIT:
Both of these things were happening for the same reason. I was using Exception and Error inside my class without prepending them with a backward slash, causing PHP to assume that Exception was in the same namespace as the one I declared for the file. Using \Exception and \Error instead resolved the issue.
The new error message is:

Error: Call to undefined method Error::decodeException()
When using unqualified class names like Exception and Error, and in the absence of a use statement to qualify the names, PHP assumes those classes are in the &quot;current&quot; namespace. The namespace of your service class appears to be Drupal\my_custom_module\Api, so that is where PHP will look for Exception and Error. When PHP does not find those classes, you will get an error.
To avoid this error, add a backslash before the class name for classes that are in the global namespace. So use \Exception and \Error instead. You do not need a use statement for either of these.
The error Fatal error: Trait 'Drupal\my_custom_module\Api\Exception' not found in /app/docroot/modules/custom/my_custom_module/src/Api/Clientname.php on line 66. is because you probably said use Exception; within the body of your class. The error Error: Class 'Drupal\my_custom_module\Api\Error' not found is because you referred to Error and not \Error.
Note that all these problems are just basic PHP issues - they are not caused by Drupal and do not involve Drupal at all. Specifically, it does not matter that you're in a service class, or that you're doing exception handling, etc.