Error exception handling in php

Source: Internet
Author: User
Tags php exception handling
Welcome to the Linux community forum and interact with 2 million technical staff. Go to PHP5 and add an error 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. every

Welcome to the Linux community forum and interact with 2 million technicians> go to PHP5 and add an error 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. every

Welcome to the Linux community forum and interact with 2 million technicians>

An error exception handling module similar to other languages is added to PHP5. 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. each try must have at least one catch. multiple catch methods can capture exceptions generated by different classes. when the try code block does not throw an exception or the catch Code cannot be found to match the exception thrown, the PHP code will continue to run after the jump to the last catch. of course, PHP allows throw again in the catch code block. when an exception is thrown, subsequent Code (the code block where the exception is thrown) will not continue to be executed, PHP will try to find the first catch that can match it. if an Exception is not captured and set_exception_handler () is not used for corresponding processing, PHP will generate a serious error and output the Uncaught Exception... (No exception is captured.

/**

* 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

}

// [2] throw

$ Message = 'If I must be run in the try {} block *** 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.