Try catch error not working

I performing actions like (trying to integrate the quickbooks sdk)

        try {
            $result = $dataService->Add($invoiceToCreate);
            echo "Invoice created successfully! Invoice ID: " . $result->Id . "<br>";
        } catch (\QuickBooksOnline\API\Exception\ServiceException $e) {
            echo "QuickBooks Error: " . $e->getMessage();
        } catch (Exception $e) {
            // Yii::error($e->getMessage(), __METHOD__); // Log error in Yii2 runtime logs
            echo "An error occurred: " . $e->getMessage();
            // If an exception occurs, retrieve the last error object
            $error = $dataService->getLastError();
            if ($error) {
                echo "The Status code is: " . $error->getHttpStatusCode() . "<br>";
                echo "The Response message is: " . $error->getResponseBody() . "<br>";
                echo "The Intuit-TID is: " . $error->getIntuitTid() . "<br>";
            } else {
                echo "An unexpected error occurred: " . $e->getMessage();
            }
        }

but I always get back the yii error handler:

Error (#8)

The following error occurred while the Web server was processing your request.
An internal server error occurred.

instead of my catch code. What am I doing wrong?

Maybe it’s an error and not an exception i.e. \Throwable?

I ended up getting it to work by adding \ in front of Exception

} catch (\Exception $e) {

What does that do? Why is that required?

1 Like