Analysis of PHP5 exception handling with example

Source: Internet
Author: User
Tags dsn error code exception handling final getmessage php code php exception handling create database

/**
* ㈠PHP5 exception Handling
*
* PHP 5 Adds an exception-handling module similar to other languages. The exception generated in the PHP code can be thrown by the throw
* statement and caught by a catch statement. Code that requires exception handling must be placed inside a try block of code to catch possible exceptions in
*. Each try must have at least one catch that corresponds to it. Use multiple catch
* To catch exceptions generated by different classes. When a try code block no longer throws an exception or cannot find a catch that matches the exception thrown by the
*, the PHP code continues after the jump to the last catch. Of course, the PHP
* allows you to throw (throw) exceptions again within the catch code block.
* When an exception is thrown, the code that follows (the code block where the exception was thrown) will not continue
and PHP will try to find the first catch that matches it. If an exception is not captured, and
* and does not use Set_exception_handler () for the appropriate processing, then PHP will produce a
* fatal error, and output uncaught exception ... (Exception not caught) message.
/
? >
/**
* exception.php
*
* ㈡PHP5 the properties and methods of the built-in exception class
* The following code is intended to illustrate the structure of the built-in exception handling class, and it is not A piece of usable code that has practical meaning.
/

Class exception{
Protected $message = ' Unknown Exception ';//exception information
protected $code = 0;//user Self , $code = 0);
Final function getMessage ();//Return exception information
Final function GetCode ();//Return exception code (code name)
Final function getFile ();//return the filename of the exception that occurred
Final function getline (); Returns the line number of the code in which the exception occurred
Final function gettrace ();//BackTrace () array
Final function gettraceasstring ();//lattice into a string of Gett Race () Information

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

? >

/**
* Syntax. php
*/

㈢ Grammatical structure and analysis

PHP has two different formats for throwing exceptions, as follows

"1" Try...catch ...
try {
Implement actions that may be unusual, such as database errors, file errors
}catch (Exception $e) {
Printing error messages
}

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

throw new Exception ($message, $code);

Learn Java's attention, PHP exception handling is not throws

? >

/**
* example.php
/
//㈣ two instances Master PHP exception handling


//Example "1" with Try...catch
/* PDO connection MySQL number According to the library, if you have not seen the PDO, first look at the PDO of the constructor, or skip the example 1 to see Example 2 */
$dsn = ' mysql:host=localhost;dbname=testdb ';
$user = ' dbuser ';
$password = ' dbpass ';

try {
$dbh = new PDO ($DSN, $user, $password);//Create database connection objects prone to exceptions
Echo ' If there's an anomaly above it can't show me ';
} catch (Pdoexception $e) {
echo ' Connection failed: '. $e->__tostring ();
}
? >

///example [2] Try ... CATHC and throw use
try {
$error = ' I throw out an exception message and jump out of a try block ';
if (Is_dir ('./tests ')) {
echo ' do sth. ';
}else{
throw new Exception ($error, 12345);
}
Echo has an exception on it. It's not my turn! ~
', "n";
} catch (Exception $e) {
echo ' catches exception: ', $e->getmessage (), $e->getcode (), n
;//show $error and 123456
}
Echo ' continued execution ';
? >

//php is easier to learn than Java, because there are too many exception classes in Java, and throws and so on
? >

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.