PHP error handling and exception handling method and example tutorial (1/2)

Source: Internet
Author: User
Tags error code error handling exception handling getmessage php error throw exception

1, built-in exception handling class.
2. Examples of capturing and handling exceptions.
3, the member function of the Exception class GetMessage ().
4, the member function of the Exception class GetFile ().
5, the member function of the Exception class Getline ().
6, display warning or error message.
7, custom error handling function.

*/

1, built-in exception handling class.

The code is as follows Copy Code

Class exception
{
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; Name of the exception that occurred
protected $line; Line number of the code where the exception occurred

function __construct ($message = null, $code = 0);

    final function getmessage ();                //Return exception information
    final function getcode ();                   //Return exception code
     Final function getfile ();                   //Returns the filename of the exception that occurred
    final function getline ();                   //Return the code line number where the exception occurred
    final function gettrace ();                  //BackTrace () array
    final function gettraceasstring ();          //Gettrace () information that has been formatted as a string

function __tostring (); A string that can be exported
}

2. Examples of capturing and handling exceptions.

The code is as follows Copy Code

Try
{
$error = ' Throw exception information and jump out of Try block <br/> ';
if (Is_dir ('./test '))
{
Echo ' detects that. /CH16 is a catalogue ';
Echo ' <br/> ';
Echo ' may continue to do some other action ';
Echo ' <br/> ';
echo ' ... ';
Echo ' <br/> ';
}
Else
{
throw new Exception ($error, 12345);
}
Echo ' Above throw exception, this line of code does not execute, instead executes catch block <br/> ';
}
catch (Exception $e)
{
Echo ' catches exception: '. $e->getmessage (). "<br/> Error code:". $e->getcode (). '    <br/> '; Show $error and 123456
Echo ' <br/> ';
}

Echo ' continued execution ';

Home 1 2 last page

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.