Exception Handling of PHP

Source: Internet
Author: User

1<?PHP2 /*3 Exception4 5 */6 //error_reporting (0);7 class Mysql {8     protected $conn;9      Public function__construct () {Ten         $this->conn = @mysql_connect(' localhost ', ' root ', ' 13456 ');//@ is an error suppressor and does not report an error One         if(!$this-conn) { A             //Satellite Reports - //In PHP, a satellite is a defined object - ///What Class of object: Exception class object the //new Exception (' Error reason ', error code) -             $e=New Exception(' Leaking oil ', 5); -             Throw $e;//Throw throw /throw out -         } +     } - } + Try{//Test and try to capture the error message A     $mysql=New Mysql();//returns the MySQL object and automatically connects to the database at      -  -}Catch(Exception $e) { -     Echo $e, ' <br/> '; -     Echo' Capture to error message <br/> '; -     Echo' Error message: ',$e->getmessage (), ' <br/> '; in     Echo' Error code: ',$e->getcode (), ' <br/> '; -     Echo' Error file: ',$e->getfile (), ' <br/> '; to     Echo' ERROR line: ',$e->getline (), ' <br/> '; + } -  the /* * Q: How can I tell if I have successfully connected to the database?  $ Answer: After returning an object, print the object's $conn property to determinePanax Notoginseng  - 2 Steps Required the 1:new Mysql + 2:if ($mysql->conn) { A } the think: When we used a function, we all returned a value, using the value to judge a variety of situations + For example, returning True/false represents success/Failure - Can we use the return value now?  $  $ */ - Var_dump($mysql);//NULL if the database connection failed - if($mysqlinstanceofMysql) { the     Echo' Create object successfully, probably connected successfully '; -}Else {Wuyi     Echo' Failed to create object, probably connection failed '; the } -?>

Exception Handling of 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.