Home > Backend Development > PHP Tutorial > PHP Advanced Programming (4/5)_PHP Tutorial

PHP Advanced Programming (4/5)_PHP Tutorial

WBOY
Release: 2016-07-13 10:27:38
Original
882 people have browsed it

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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815980.htmlTechArticleSPL provides a series 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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template