Home > Article > Backend Development > PHP error handling and exception handling methods and example tutorials (1/2)_PHP tutorial
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.
// 2. Example of catching and handling exceptions.class exception
function __construct($message = null, $code = 0);
{
Protected $message = 'unknown exception'; // Exception message
protected $code = 0; //User-defined exception code
protected $file; // The file name where the exception occurred
protected $line;final function getmessage(); // Return exception message
final function getcode(); // Return exception code
function __tostring(); // Outputable string
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
}
try1 2{
echo 'continue execution';
$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 '
';
}