Php error handling

Source: Internet
Author: User
Tags php exception handling
Php exception handling preliminary learning (transfer) & lt ;? Php/*** PHP exception handling *** PHP5 adds an exception handling module similar to other languages. Exceptions in PHP code can be thrown by throw * statements and captured by catch statements. All codes that require exception handling must be placed in the try code block to capture possible exceptions. Preliminary Study on every php exception handling (transfer)
/**
* PHP 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.
*/
?>

/**
* Exception. php
*
* Attributes and methods of built-in exception classes in PHP5
* 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-able method
Function _ toString (); // output string
}
?>

/**
* Syntax. php
*/

// 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 and write the log or process it again
}

// [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
?>
/**
* Example. php
*/
// Master PHP exception handling for two instances


// 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 ();
}
?>

// 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 !~
', "\ N ";
} Catch (Exception $ e ){
Echo 'capture exception: ', $ e-> getMessage (), $ e-> getCode (), "\ n
"; // Display $ error and 123456
}
Echo 'continue execution ';
?>

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.