Home >Backend Development >PHP Tutorial >PHP error handling and exception handling methods and example tutorials (1/2)_PHP tutorial

PHP error handling and exception handling methods and example tutorials (1/2)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:57:49800browse

php error handling and exception handling methods and example tutorials In program development, error handling is very important. Today, this article will tell him about the error handling function in PHP development and give examples to illustrate the importance of error handling.

php tutorial error handling and exception handling methods and example tutorials
In program development, error handling is very important. Today, this article will tell him about the error handling function in PHP development and give examples to illustrate the importance of error handling.

1. Built-in exception handling class.
2. Examples of catching and handling exceptions.
3. The member function getmessage() of the exception class.
4. The member function getfile() of the exception class.
5. The member function getline() of the exception class.
6. Display warning or error messages.
7. Custom error handling function.

*/

// 1. Built-in exception handling class.

class exception
{
Protected $message = 'unknown exception'; // Exception message
protected $code = 0; //User-defined exception code
protected $file; // The file name where the exception occurred
protected $line;

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

final function getmessage(); // Return exception message

final function getcode(); // Return exception code
final function getfile(); // Return the file name where the exception 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 into a string

function __tostring(); // Outputable string

}

// 2. Example of catching and handling exceptions.

try

{
$error = 'Throw exception information and jump out of try block
';
If(is_dir('./test'))
{
echo 'Detected that ../ch16 is a directory';
echo '
';
echo 'Maybe continue to do other operations';
echo '
';
echo '....';
echo '
';
}
else
{
          throw new exception($error,12345);
}
echo 'If the above throw exception occurs, this line of code will not be executed, and the catch block will be executed instead
';
}
catch(exception $e)
{
echo 'Catch exception: ' . $e->getmessage() . "
Error code: " . $e->getcode().'
'; //Show $ error and 123456
echo '
';
}

echo 'continue execution';

1 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632056.htmlTechArticlephp Error handling and exception handling methods and example tutorials In program development, error handling is very important. Today this article will tell him about error handling letters in PHP development...
Statement:
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