Analysis of PHP5 exception handling using instances _ PHP Tutorial

Source: Internet
Author: User
Tags dsn php exception handling
Analyze the PHP5 exception handling using an instance. <? Php *** ■ (I) PHP5 exception handling ** PHP5 adds an exception handling module similar to other languages. Exceptions in PHP code can be thrown by the throw * statement and caught. <? Php
/**
* ■ (I) PHP5 exception handling
*
* PHP 5 adds an exception handling module similar to other languages. Exceptions generated in PHP code can be throw
* The statement is thrown and captured by the catch statement. All codes that require exception handling must be placed in the try code block
* Detects possible exceptions. Each try must have at least one catch corresponding to it. Use multiple catch
* Exceptions generated by different classes can be captured. When the try code block does not throw an exception or the catch cannot be found
* When an exception is thrown, the PHP code will continue to be executed after the jump to the last catch. Of course, PHP
* Throw can be thrown again in the catch code block.
* When an exception is thrown, the code after the exception (note: refers to the code block in which the exception is thrown) will not continue.
* Execute, and PHP will try to find the first catch that can match it. If an exception is not caught
* If set_exception_handler () is useless, PHP will generate
* Serious errors, and the output of the Uncaught Exception... (no Exception is captured) prompt information.
*/
?>
<? Php
/**
* Exception. php
*
* ■ (II) attributes and methods of the PHP 5 built-in exception classes
* The following code only describes the structure of the built-in exception handling class. it is not a practical usable code.
*/

Class Exception {
Protected $ message = 'unknown exception'; // exception information
Protected $ code = 0; // custom exception code
Protected $ file; // file name with an exception
Protected $ line; // The code line number with an exception

Function _ construct ($ message = null, $ code = 0 );
Final function getMessage (); // returns exception information
Final function getCode (); // return the exception code (code)
Final function getFile (); // returns the file name with an exception
Final function getLine (); // return the code line number in which an exception occurs.
Final function getTrace (); // backtrace () array
Final function getTraceAsString (); // getTrace () information formatted as a string

Reload method
Function _ toString (); // output string
}

?>

<? Php
/**
* Syntax. php
*/

// ■ (3) syntax structure and analysis

// PHP has two formats for throwing an exception:

// [1] try... catch...
Try {
// Perform operations that may be abnormal, such as database errors and file errors
} Catch (Exception $ e ){
// Print the error message
}

// [2] throw
$ Message = 'I must be run in the try {} block. if an exception occurs, I ($ message) will be returned (passed) to catch () for example, the above $ e ';
$ 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 that PHP exception handling does not throws

?>

<? Php
/**
* Example. php
*/
// ■ (4) two instances master PHP exception handling


// Example [1] use try... catch
/* PDO connects to the mysql database. if you have not read the PDO, first check the PDO constructor. otherwise, skip example 1 and see Example 2 */
$ Dsn = 'MySQL: host = localhost; dbname = testdb ';
$ User = 'dbuser ';
$ Password = 'dbpass ';

Try {
$ Dbh = new PDO ($ dsn, $ user, $ password); // An exception may occur when you create a database connection object.
Echo 'If an exception occurs above, I cannot be displayed ';
} Catch (PDOException $ e ){
Echo 'connection failed: '. $ e->__ toString ();
}
?>

<? Php
// Example [2] try... cathc and throw
Try {
$ Error = 'I throw exception information and exit the try block ';
If (is_dir ('./tests ')){
Echo 'Do something .';
} Else {
Throw new Exception ($ error, 12345 );
}
Echo 'if there is an exception above, it won't turn to me !~ <Br/> ', "n ";
} Catch (Exception $ e ){
Echo 'capture exception: ', $ e-> getMessage (), $ e-> getCode (), "n <br/>"; // Display $ error and 123456
}
Echo 'continue execution ';
?>

<? Php
// PHP processing is easier to learn than JAVA, because JAVA has too many exception classes and throws.
?>

Http://www.bkjia.com/PHPjc/371514.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371514.htmlTechArticle? Php/*** ■ (I) exception handling in PHP5 ** PHP 5 adds an exception handling module similar to other languages. Exceptions in PHP code can be thrown by the throw * statement and caught...

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.