PHP error handling and exception handling methods and examples tutorial in program development, error handling this piece is very important, today this article to tell him about in PHP development, error handling functions and examples of the importance of error handling.
PHP Tutorial error handling and exception handling methods and examples tutorial
In program development, error handling this piece is very important, today this article to tell him about in PHP development, error handling function and illustrate the importance of error handling.
1, built-in exception handling class.
2. Examples of catching and handling exceptions.
3. The member function of the exception class is GetMessage ().
4. The member function of the exception class is GetFile ().
5. The member function of the exception class is Getline ().
6. Display a warning or error message.
7. Custom error handling functions.
*/
1, built-in exception handling class.
Class exception
{
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; The file name of the exception that occurred
protected $line; The 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 file name of the exception that occurred
Final function getline (); Returns the line number of the code where the exception occurred
Final function gettrace (); BackTrace () array
Final function gettraceasstring (); Gettrace () information that has been rasterized into a string
function __tostring (); A string that can be output
}
2. Examples of catching and handling exceptions.
Try
{
$error = ' throws exception information and jumps out of try Block
';
if (Is_dir ('./test '))
{
Echo ' detected: /CH16 is a directory ';
Echo '
';
Echo ' may continue to do some other operations ';
Echo '
';
echo ' ... ';
Echo '
';
}
Else
{
throw new Exception ($error, 12345);
}
Echo ' Above throw exception, this line of code will not execute, instead execute catch block
';
}
catch (Exception $e)
{
Echo ' catch exception: '. $e->getmessage (). "
Error code: ". $e->getcode (). '
'; Show $error and 123456
Echo '
';
}
Echo ' continues to execute ';
1 2
http://www.bkjia.com/PHPjc/632056.html www.bkjia.com true http://www.bkjia.com/PHPjc/632056.html techarticle PHP error handling and exception handling methods and examples tutorial in program development, error handling this piece is very important, today this article to tell him about in PHP development, error handling letter ...