Exception handling in Magento 2

May 6, 2020 0 Comments
private $logger;

public function __construct(\Psr\Log\LoggerInterface $logger)
{
    $this->logger = $logger;
}

And then in your catch statement:

public function doSomething()
{
    try {
        /* Some logic that could throw an Exception */
    } catch (\Exception $e) {
        $this->logger->critical($e->getMessage());
    }
}

Leave A Comment

To Top