PHP Exception Handling

Source: Internet
Author: User
Tags getmessage php exception handling try catch

<?php
/* Starting with PHP5, PHP supports exception handling and exception handling is an important feature of object-oriented, PHP
Exceptions in the code are thrown by a throw, and after the exception is thrown, the following code will not execute
Exception throw is used to notify the client when a location error is encountered, or if a pre-set condition is not met
In order to carry out other related processing, not to make the program direct error interruption
When a try catch is used in the code, the thrown exception is caught in the catch, otherwise it will be interrupted directly.
/*
1. Basic grammar
try{
Code with errors or exceptions that may occur
Catch represents capture, exception is a defined exception class for PHP
} catch (Exception $e) {
For exception handling, method:
1, self-treatment
2. Do not process, throw it again
}*/
/*
2. Processing procedures shall include:
Try-the function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.
Throw-this specifies how the exception is triggered. Note: Each "throw" must correspond to at least one "catch", which can of course correspond to multiple "catch"
Catch-The catch code block catches the exception and creates an object that contains the exception information. */
function Checknum ($number) {
if ($number > 1) {
throw new Exception ("exception hint-number must be less than or equal to 1");
}
return true;
}
try{
Checknum (2);
Echo ' If you can see this hint, it means your number is less than or equal to 1 ';
}catch (Exception $e) {
Echo ' catch exception: '. $e->getmessage ();
}
/*php has many exception handling classes, where exception is the base class for all exception handling
Exception has several basic properties and methods, including the following:
Message content for exception messages
Code exception Codes
File name that throws an exception
Line throws an exception in the number of rows in the file
Among the methods commonly used are:
Gettrace getting information on exception tracking
Gettraceasstring string that gets the exception trace information
GetMessage getting error messages
If necessary, you can create the exception-handling class for the sub-theorem by inheriting the exception class */
Class MyException extends exception{
function GetInfo () {
Return ' custom error message ';
}
}
try{
The function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.
throw new MyException (' Error ');
This specifies how the exception is triggered. Note: Each "throw" must correspond to at least one "catch", which can of course correspond to multiple "catch"
}catch (Exception $e) {
The catch block catches the exception and creates an object that contains the exception information
echo $e->getinfo ();
echo $e->getmessage ();
}

/*

After the exception is captured, we can get the exception information through the exception handling object, we already know the way to capture, and get the basic error information.
In real-world applications, we usually get enough exception information and write it to the error log.
We need to record the file name, line number, error message, exception tracking information, etc. to the log in order to debug and fix the problem.
*/
try{
throw new Exception (' wrong ');

}catch (Exception $e) {
echo ' ERROR ';
$msg = ' Error: '. $e->getmessage (). " \ n ";
$msg. = $e->gettraceasstring (). " \ n ";
$msg. = ' exception line: '. $e->getline (). " \ n ";
$msg. = ' exception is located in file: '. $e->getfile (). " \ n ";
File_put_contents (' Error.log ', $msg)
}

?>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Exception Handling

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.