Home >Backend Development >PHP Tutorial >Analyze PHP5 exception handling with examples_PHP tutorial

Analyze PHP5 exception handling with examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:52:52884browse

<?php
/**
* ■Exception handling in PHP5
*
* PHP 5 adds an exception handling module similar to other languages. Exceptions generated in PHP code can be thrown
* statement throws and is caught by a catch statement. Code that requires exception handling must be placed within the try code block, ending with
* Catch possible exceptions. Every try must have at least one corresponding catch. Use multiple catches
* Can catch exceptions generated by different classes. When the try code block no longer throws an exception or cannot find a matching catch
* When an exception is thrown, the PHP code will continue executing after jumping to the last catch. Of course, PHP
* Allow exceptions to be thrown again within catch blocks.
* When an exception is thrown, the subsequent code will not continue
* is executed, and PHP will try to find the first matching catch. If an exception is not caught and
* If there is no need to use set_exception_handler() for corresponding processing, then PHP will generate a
* A serious error, and the prompt message Uncaught Exception... (uncaught exception) is output.
*/
?>
<?php
/**
* Exception.php
*
* ■㈡The properties and methods of the built-in exception class in PHP5
* The following code is only to illustrate the structure of the built-in exception handling class. It is not a usable code with practical significance.
*/

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; // Code line number where the exception occurred

function __construct($message = null, $code = 0);
final function getMessage(); // Return exception message
final function getCode(); // Return exception code (code name)
final function getFile(); // Return the file name where the exception occurred
final function getLine(); // Returns the code line number where the exception occurred
final function getTrace(); // backtrace() array
final function getTraceAsString(); // The getTrace() information that has been formatted into a string

Overloadable methods
function __toString(); // Outputable string
}

?>

<?php
/**
* syntax .php
*/

//■㈢Grammar structure and analysis

//PHP has two formats for throwing exceptions, as follows

//【1】try...catch...
try {
//Perform abnormal operations, such as database errors and file errors
}catch (Exception $e){
//Print error message
}

//【2】throw
$message='I must be run in the try{} block. If an exception occurs, I ($message) will be returned (passed) to the instance of the exception object in catch(), such as $e above';
$code=123; //Error code number, you can use $e->getCode(); in the catch block to return my value 123, so that I can customize the error code number

throw new Exception($message,$code);

//Note when learning JAVA, PHP exception handling does not have throws

?>

<?php
/**
* Example.php
*/
//■㈣Two examples to master PHP exception handling


//Example [1] use try...catch
/* PDO connects to the mysql database. If you have not seen PDO, first look at the constructor of PDO, or skip Example 1 and look at Example 2 */
$dsn = 'mysql:host=localhost;dbname=testdb';
$user = 'dbuser';
$password = 'dbpass';

try {
$dbh = new PDO($dsn, $user, $password); //Creating a database connection object is prone to exceptions
echo 'If there is an exception above, I will not be displayed';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->__toString();
}
?>

<?php
//Example[2] try..cathc and throw are used together
try {
$error = 'I threw an exception message and jumped out of the try block';
if(is_dir('./tests')){
echo 'do sth.';
}else{
throw new Exception($error,12345);
}
echo 'If there is an exception above, it won't be my turn! ~<br />',"n";
} catch (Exception $e) {
echo 'Catch exception: ', $e->getMessage(),$e->getCode(), "n<br />"; //Display $error and 123456
}
echo 'Continue execution';
?>

<?php
//PHP's processing is much easier to learn than JAVA, because JAVA has too many exception classes, throws, etc.
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371514.htmlTechArticle<?php /** * ■㈠Exception handling in PHP5* * PHP 5 adds exceptions similar to other languages processing module. Exceptions generated in PHP code can be thrown by the throw * statement and caught by the catch statement...
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