PHP exception handling definition and how to use it

Source: Internet
Author: User
Tags php exception handling
This article mainly introduces the definition and use of PHP exception handling, a more detailed analysis of the PHP exception handling definition, use methods and related considerations, the need for friends can refer to the following

Specific as follows:

<?PHP//PHP5 provides basic exception handling classes that can be used directly, without having to redefine//class exception//{///protected $message = ' Unknown Exception ';//exception information//prot ected $code = 0; User-defined exception code//protected $file; The filename of the exception occurred//protected $line; The code line number//function __construct ($message = null, $code = 0) of the exception that occurred;//Final function getMessage (); Returns the exception information//FINAL function getcode (); Returns the exception code//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 is *///function __tostring ()////////*; Output string//}?>

<?php//defines a top-level exception handling function my_exception ($e) {  echo "I am the top exception handling". $e->getmessage (); Modify the default top-level exception handler (Set_exception_handler) ("my_exception");//We use the anomaly mechanism try{  addUser ("shunping");  UpdateUser ("Xiaoming1");} Catch is used to catch exceptions, Exception is the exception class (PHP is defined as a class) catch (Exception $e) {  //Return exception information  echo "Return exception information (failure message) =". $e GetMessage (). " <br/> ";  echo "Return Exception code =". $e->getcode (). " <br/> ";  echo "Returns the file name of the exception that occurred =". $e->getfile (). " <br/> ";  echo "Returns the line number of the code where the exception occurred =". $e->getline (). " <br/> ";  echo "=". $e->gettrace (). " <br/> "; BackTrace () array  //can continue to throw, this will start PHP default exception handler to process  //You can also define a top-level exception handler, handle the  throw $e;} function AddUser ($username) {  if ($username = = "Shunping") {    //add OK    echo "Ok1";  } else{    //Add error    //throw exception.    throw new Exception ("Add Failed");}  } function UpdateUser ($username) {  if ($username = = "Xiaoming") {    //normal modify    echo "Ok2";  } else{    //Flat Throw exception    throw new Exception ("Modify failed");}  }? >

Precautions for exception use:

Through the above case, we can see that the use of

try{    //code}catch (Exception $e) {    //pair exception handling}

This way, you can control errors more effectively. So in the development of a lot of use.

1. When an exception is caught, subsequent code in the try{} block does not continue to execute.
2. If there is an exception that occurs, but you do not have catch capture, you are prompted with a Uncaught Exception.(系统.)
3. When catching an exception, you can handle it, or you can not handle it, and you can
throw new Exception("信息");

4. You can customize the exception class

Class MyException extends Exception {}

5. Use multiple catch code blocks to capture different kinds of exceptions

try{    //code ....  First sentence  //second sentence}catch (pdoexception $e) {    //}catch (Exception $e) {|}

6. Also simple to use Exception capture

Code Description:

<?php    //Defines an exception    class MyException1 extends exception{    }    class MyException2 extends exception{    }    function A () {       throw new MyException1 ("a");    }    function B () {       throw new MyException2 ("B")    }    function C () {       try{           A ();//throw MyException1           B ();//Throw MyException2       }catch (Exception1 $e 1) {           $e 1->getmessage ();       } catch (Exception2 $e 2) {           $e 2->getmesage ();       }    }? >

Rules for exceptions

The code that requires exception handling should be placed inside a try code block to catch a potential exception.
Each try or throw code block must have at least one corresponding catch code block.
You can use multiple catch blocks to catch different kinds of exceptions.
Exceptions can be thrown (thrown again) in a catch code block within a try code block.
In short: If an exception is thrown, it must be captured. or use the top-level exception handler.
If not thrown, even if an exception is not caught, it will not be processed

Related recommendations:

PHP Exception handling and error handling method sharing

What is PHP exception handling

Customization of PHP exception handlers

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.