Question

I found different cases to use Magento throw exception, I want to know what is the difference between (Mage_Core_Exception $e) and (Exception $e) ? I found What's the preferred way of throwing exceptions in Magento? answer, but it's not precise for me. Is Mage::throwException is the correct one and that we should use ?

Was it helpful?

Solution

This is just PHP type hinting ... the one expects $e to be element of class Exception, the other one expects Mage_Core_Exception.

Since class Mage_Core_Exception extends Exception the only "differences" are three changed methods.

For Mage::throwException() see app/Mage.php. At the end it is throws a new exception of class Mage_Core_Exception (thats child of PHPs Exception).

public static function throwException($message, $messageStorage = null)
{
    if ($messageStorage && ($storage = self::getSingleton($messageStorage))) {
        $storage->addError($message);
    }
    throw new Mage_Core_Exception($message);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top