I have tried this but this haven't worked for me

    if (isset($this->_areaCode)) {
        throw new MagentoFrameworkExceptionLocalizedException(
            new MagentoFrameworkPhrase('Area code is already set')
    $this->_areaCode = $code;

By catching the exception Magento throws and silently ignoring it.

public function __construct(
    \Magento\Framework\App\State $appState,
) {
    try {
    } catch (MagentoFrameworkExceptionLocalizedException $e) {
        // intentionally left empty


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$area = $objectManager->get('Magento\Framework\App\State');
if (!$area->getAreaCode()) {
$currentAreaCode = $area->getAreaCode();
| improve this answer | |
  • Using objectManager is not recommended and its considered bad practise. Inject the desired class into your own class and then use it without calling the objectManageron it. – Petar Borisovski Oct 15 at 7:20

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.