PHP exception handling mechanism

Source: Internet
Author: User
Tags getmessage php exception handling

• The "@" symbol tells PHP to suppress all errors caused by the contents of our code behind it.

<?php

Try

{

throw new Exception (' An error has occurred! ', 42);

}

catch (Exception $e)

{

echo ' number '. $e->getcode (). ': '. $e->getmessage ();

}

?>

The exception exception class is provided in PHP 5, which provides some built-in methods in addition to constructors:

GetCode (): Returns the code passed to the constructor getmessage (): Returns the message passed to the constructor GetFile (): Returns the full path of the code file that generated the exception getline (): Returns the line number of the code that generated the exception in the code file · Gettrace (): Returns an array containing the code fallback path that generated the exception gettraceasstring (): Returns the same information as the Gettrace () direction, which is formatted as a string. _tostring (): Allows a simple display of a exception object and gives the information that all of the above methods can provide.

<?php

Class MyException extends exception

{

function _tostring ()

{

Return ' <table><tr><td>exception '. $this->getcode ().

': '. $this->getmessage (). ' <br/>in '. $this->getfile ().

' On line '. $this->getline (). ' </td></tr></table> ';

}

}

Try

{

throw new MyException (' Error! ', 42);

}

catch (MyException $e)

{

Echo $e; The _tostring () method is called automatically.

}

?>

PHP exception handling mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.