PHP captures specific types of exceptions in a detailed

Source: Internet
Author: User
Exception capture in many IDE tools now can be easily added with shortcut keys, to prevent users from seeing their own errors or even inexplicably crashes, resulting in a bad user experience. Even showing a friendly crash hint is much better than showing error:xxxx xxxxxxxxx directly. This article will explain in detail how PHP throws exceptions and catches specific types of exceptions, and we hope to help.

What is an exception?

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

Exception handling is used to change the normal process 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 the pre-defined exception handler function

Depending on the situation, the processor may start executing the code again from the saved code state, terminating the script execution, or continuing the script from another location in the code

We will show you different error handling methods:

Basic use of exceptions

Creating a custom exception handler

Multiple exceptions

To re-throw an exception

Setting the top-level exception handler

The ultimate goal of PHP throwing exceptions and catching specific types of exceptions is to give the corresponding workaround 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 * Package Weight Exception 5 */6 class Heavyparcelexception extends Exception {} 7 8/** 9 * Package Class */11 class Parcel {12 13/**1 4 * Parcel Shipping Destination Address */16 public $address; 17 18/**19 * Package Weight: */21 public $weight; 22}23 24/**25 * Dispatch clerk: */27 C     Lass Courier {28 29/** 30 * Shipping */32 Public Function ships (Parcel $parcel) {$//check we have a address34 If the destination of the package is null (empty ($parcel->address)) {$ throw new Exception (' Address not Specified (not filled in) '); PNS}//check the WEIGHT40//if the weight exceeds 541 if ($parcel->weight > 5) {$ throw new Heavyparcele Xception (' Parcel exceeds courier limit (parcel exceeds shipping limit)! '); //otherwise we ' re coll46 return true;47}48}49 $myCourier = new Courier (); Wuyi $parcel = new Pa Rcel ();//add the address if we have it in order to test here does not fill in addresses $parcel->weight = 7;54 try {$myCourier->ship ($parcel); 5 6 echo "Parcel shipped"; (Heavyparcelexception $e) {//Capture heavyparcelexception does not write the type name of this exception, ran to the ordinary exception thrown to the "Parcel Weight error (weight error):". $e->getmessage ();//redirect them to choose another courier60} catch (Exception $e) {echo "someting went Wron g (address Error): ". $e->getmessage ();//exit so we don ' t try to proceed any further63 exit;64}65 echo ' <br/> '; $a = 123;67 E Cho $a;

Code execution order starting with 54 lines:

>

>

(The ship method first checks that the address is empty, this will be thrown Exception , not 57 rows HeavyParcelException ) >

60 (captured Exception ) >

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

Tips:

I feel that the most important thing about this piece is figuring out the order in which the code executes. Write a few paragraphs, then change a run.

1. The order in which the captures are to be seen in try the code throw is what type Exception , and then the order in which they are viewed catch .

A 2.57-line capture is a specific type HeavyParcelException that cannot be written incorrectly, or it can be problematic to write Exception . You can try it yourself.

1) For example, if the address is not empty and 57 lines are written HeavyParcelException111 , it will be captured in 60 rows of its parent class object, with a weight error. That's not what we want.

2) For example, the address is empty, 57 lines written Exception , will throw an address error, but the capture is originally responsible for weight catch . And that's not what we want.

Related recommendations:

Customization of the PHP exception handler

How PHP implements image recognition

How PHP implements process lock and multi-process

Related Article

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.