PHP throws exceptions and captures specific types of exceptions _php tips

Source: Internet
Author: User
Tags error handling exception handling getmessage

What is an exception?

PHP 5 provides a new object-oriented error-handling approach.

Exception handling is used to change the normal flow of a script when a specified error (exception) condition occurs. This condition is called an exception.

When an exception is triggered, it usually occurs:

Current code state is saved

Code execution is switched to a predefined exception handler function

Depending on the situation, the processor may restart execution of the code from the saved code State, terminate the script execution, or continue the script from another location in the code

We will show different error handling methods:

Basic use of exceptions

Creating a custom exception handler

Multiple exceptions

Throwing Exceptions again

To set the top-level exception handler

PHP throws an exception and catches a specific type of exception the ultimate goal is to give the corresponding solution, so that the code can continue to run.

Test environment for this article: PHP5.5.36 Safari 9.1.2

 1 <?php 2 header ("content-type:text/html;
 Charset=utf-8 "); 
3/** 4 * Parcel weight anomaly 5/6 class Heavyparcelexception extends Exception {} 7 8/** 9 * Package Class * * All class Parcel {12
13/** 14 * Parcel Mailing Destination Address * * Public $address;
17 18/** 19 * Package Weight * * Public $weight; 22} 23 24/** 25 * Dispatch Clerk/class Courier {28 29/** 30 * Shipping/Public Function ship (Parcel $parce L) {//check We have a address 34//If the destination of the parcel is empty if (empty ($parcel->address)) {throw new Exce
Ption (' address is not Specified (addresses not filled)! '); //check the weight 40//If the weight is more than 5 ' if ($parcel->weight > 5) {throw new Heavypa
Rcelexception (' Parcel exceeds courier limit (parcel exceeding shipping limit)! ');
//otherwise we ' re coll to true;
$myCourier = new Courier ();
Wuyi $parcel = new Parcel ();
//add the address if we have it for testing here do not fill in addresses $parcel->weight = 7; The try {$myCourier->ship ($parcel);
The echo "Parcel shipped"; catch (Heavyparcelexception $e) {//capture heavyparcelexception does not write the type name of this exception, and ran to the ordinary exception thrown to the echo "Parcel weight err or (weight error): ".
$e->getmessage (); them to choose another courier//redirect catch (Exception $e) {"echo" someting went (address error): ".
$e->getmessage ();
//exit so we don ' t try to proceed any further;
The echo ' <br/> ';
$a = 123; echo $a;

Code execution order starting at line 54:

>

>

(The ship method first checks that the address is empty, which is thrown Exception , not 57 lines HeavyParcelException ) >

60 (captured Exception ) >

616263 output address error exit ; 65 to 67 lines are not output

Tips:

I feel that the most important thing is to figure out the sequence of code execution. Write a few paragraphs, and then change to run a run.

1. The order of capture depends on try throw which type of code is in and then the Exception order is read catch .

2.57 lines are caught with a specific type that HeavyParcelException cannot be written incorrectly, or Exception there may be problems with writing. You can try it on your own.

1 For example, the address is not empty, 57 lines written HeavyParcelException111 , will be, in 60 rows of its parent class object caught, weight error. That's not what we want.

2 For example, the address is empty, 57 lines written Exception , will throw the address error, but the capture is responsible for the weight catch . This is not what we want.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.