SPL 提供了一系列標准異常。日常的使用中我們應該根據需求科學的使用它們,來使我們的程序更加健壯。LogicException 是從 Exception 基類派生的,沒有添加任何附加方法。拋出邏輯異常和拋出標准一次的方法類似,區別在於邏輯異常須在應用程序編寫有誤時才拋出。下面演示下LogicException類的使用。
class App
{
protected $_loaded = false;
protected $_name;
public function start()
{
$this->_loaded = true;
$this->_name = 'unzin';
}
public function GetName()
{
if(!$this->_loaded)
{
throw new LogicException("Error Processing Request", 1);
}
return $this->_name;
}
}
try
{
$APP = new App();
echo $APP -> GetName();
}
catch(LogicException $e)
{
echo $e->getMessage();
}