Home >Backend Development >PHP Tutorial >PHP Advanced Programming (4/5)_PHP Tutorial
SPL provides a set of standard exceptions. In daily use, we should use them scientifically according to our needs to make our programs more robust. LogicException is derived from the Exception base class without adding any additional methods. Throwing logical exceptions is similar to throwing standard once, except that logical exceptions must be thrown only when the application is written incorrectly. The following demonstrates the use of the LogicException class.
<span>class</span><span> App { </span><span>protected</span> $_loaded = <span>false</span><span>; </span><span>protected</span><span> $_name; </span><span>public</span><span> function start() { $</span><span>this</span>->_loaded = <span>true</span><span>; $</span><span>this</span>->_name = <span>'</span><span>unzin</span><span>'</span><span>; } </span><span>public</span><span> function GetName() { </span><span>if</span>(!$<span>this</span>-><span>_loaded) { </span><span>throw</span> <span>new</span> LogicException(<span>"</span><span>Error Processing Request</span><span>"</span>, <span>1</span><span>); } </span><span>return</span> $<span>this</span>-><span>_name; } } </span><span>try</span><span> { $APP </span>= <span>new</span><span> App(); echo $APP </span>-><span> GetName(); } </span><span>catch</span><span>(LogicException $e) { echo $e</span>-><span>getMessage(); }</span>