An example analysis of PHP5 exception handling _php Tutorial

Source: Internet
Author: User
Tags dsn php exception handling
<?php
/**
* Exception Handling for ㈠PHP5
*
* PHP 5 Adds an exception handling module similar to other languages. Exceptions generated in PHP code can be thrown
* Statements are thrown and captured by catch statements. The code that requires exception handling must be placed inside a try code block to
* Catches a possible exception. Each try must have at least one catch corresponding to it. Using multiple catch
* You can capture exceptions that are generated by different classes. When a try code block no longer throws an exception or cannot find a catch to match
* When the exception is thrown, the PHP code will continue to execute after jumping to the last catch. Of course, PHP
* Allows the throw (throw) exception to be thrown again within the catch code block.
* When an exception is thrown, the code behind it (the code block where the exception was thrown) will not continue
* Executes, and PHP tries to find the first catch to match. If an exception is not captured, and
* And without using Set_exception_handler () for the appropriate processing, then PHP will produce a
* A serious error, and output uncaught Exception ... (The exception is not caught) prompt information.
*/
? >
<?php
/**
* exception.php
*
* ㈡PHP5 properties and methods of the built-in exception classes
* The following code is only for the purpose of explaining the structure of the built-in exception-handling class, which is not a meaningful piece of code available.
*/

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 (codename)
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

Methods that can be overloaded
function __tostring (); A string that can be output
}

? >

/**
* Syntax. php
*/

㈢ Grammatical structure and analysis

PHP has two types of exception-throwing formats, as follows

"1" Try...catch ...
try {
Implement operations that may be abnormal, such as database errors, file errors
}catch (Exception $e) {
Print error message
}

"2" throw
$message = ' I must be running in the try{} block, I ($message) will be returned (passed) to an instance of the exception object in the catch () such as the above $e ';
$code = 123; The error code number, which can be $e->getcode () in the Catch block, returns my value of 123 so that I can customize the error code number

throw new Exception ($message, $code);

Learn Java Note that PHP exception handling does not throws

? >

/**
* example.php
*/
㈣ Two examples of PHP exception handling


Example "1" with Try...catch
/* PDO connect to MySQL database, if you have not seen PDO, first look at the PDO constructor, or skip example 1 to see Example 2 */
$dsn = ' Mysql:host=localhost;dbname=testdb ';
$user = ' Dbuser ';
$password = ' Dbpass ';

try {
$DBH = new PDO ($DSN, $user, $password); Creating database connection objects is prone to exceptions
Echo ' If there is an exception above it will not show me ';
} catch (Pdoexception $e) {
Echo ' Connection failed: '. $e->__tostring ();
}
? >

example [2] Try: CATHC and throw together
try {
$error = ' I throw an exception message and jump out of the try Block ';
if (Is_dir ('./tests ')) {
Echo ' do sth. ';
}else{
throw new Exception ($error, 12345);
}
Echo ' There's something wrong with it, it's not my turn! ~
', "n";
} catch (Exception $e) {
Echo ' catch exception: ', $e->getmessage (), $e->getcode (), "N
"; Show $error and 123456
}
Echo ' continues to execute ';
? >

PHP's processing is much easier to learn than Java, because Java has too many exceptions, throws, etc.
? >

http://www.bkjia.com/PHPjc/371514.html www.bkjia.com true http://www.bkjia.com/PHPjc/371514.html techarticle ...

  • Related Article

    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.