0

I have a try catch block where inside of try I navigate to different set of childHtml based on a if-else in the parent .phtml file and if there is any error, I need to catch them. Problem is, the exceptions are not getting caught by the try catch block. Any issues inside the childHtml is getting added to the exception.log. Following is the try-catch block.

            try {
            if ($var1)
                echo $block->getChildHtml('template_1');
            elseif($var2) {
                echo $block->getChildHtml('template_2');
            } elseif ($var3) {
                echo $block->getChildHtml('template_3');
            } else {
                echo $block->getChildHtml('template_4');
            }
            
            } catch (\Exception $e) {
                //Some stuff.
            }
            ?>

For instance, I have an error in template_1, I expect it to be catched inside the above catch block. It is happening if it is in a developer mode. But not in a prod mode. Not sure if that is the problem here. Can anyone guide me out of this please. Thanks in advance.

1 Answer 1

0

try

{
    if ($var1)
        $child = $block->getChildHtml('template_1');
        throw new \Exception(__('Errors'));
    elseif ($var2) {
        $child = $block->getChildHtml('template_2');
        throw new \Exception(__('Errors'));
    } elseif ($var3) {
        $child = $block->getChildHtml('template_3');
        throw new \Exception(__('Errors'));
    } else {
        $child = $block->getChildHtml('template_4');
        throw new \Exception(__('Errors'));
    }

} catch (\Exception $e) {
    //Some stuff.
}
?>
1
  • Works like a charm! Thank you!
    – Nithin
    Jul 13, 2021 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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