1

When using coding standard checks my attention is often drawn to the use of exceptions. Generally when I'm putting something together I use standard exceptions and later code something more specific.

The general consensus from code quality checks is that \Exception should not be used but instead more specific types of exception. I understand that part.

Some of the exceptions are obvious what the correct usage is. For example an entity not found exception NoSuchEntityException.

I'm unsure on LocalizedException. What is the difference between this and standard Exception. And generally when should it be used?

2

LocalizedException

The \Magento\Framework\Exception\LocalizedException varies from \Exception in the way that it uses Magentos \Magento\Framework\Phrase for the raw message that is going to be displayed or logged.

This makes sure that you can display the messages in the language of your store.

From the Technical guidelines:

5.17. Exceptions which need to be displayed to the user MUST be sub-types of LocalizedException. Any other types of exceptions MUST be wrapped with LocalizedException before being displayed to the user.

For further guideline on how to use Exceptions in Magento you can refer to the Exceptions part of the technical guidelines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.