Exception handling for errors in PHP

Source: Internet
Author: User
Tags dsn php exception handling

Xiao Han | reads: 1636 an error exception handling module similar to other languages has been added to PHP5. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement. The code that requires exception handling must be placed in the PHP5 to add an error exception handling module similar to other languages. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement. The code that requires exception handling must be placed inside a try code block to catch possible exceptions. Each try must have at least one catch corresponding to it. You can use multiple catches to catch exceptions that are generated by different classes. When the try code block no longer throws an exception or cannot find a catch that matches the thrown exception, the PHP code resumes execution after jumping to the last catch. Of course, PHP allows you to throw (throw) exceptions again within a catch code block. When an exception is thrown, the code behind it (the code block where the exception was thrown) will not continue, and PHP will attempt to find the first catch to match it. If an exception is not captured and does not use Set_exception_handler () for the appropriate processing, then PHP will produce a serious error and output uncaught exception ... (The exception is not caught) prompt information.

/**
* 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 ';
?>

Exception handling for errors in PHP

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.