SoFunction
Updated on 2025-03-10

Detailed explanation of php throwing exceptions and catching specific types of exceptions

What is an exception?

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

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

Usually happens when an exception is triggered:

The current code status is saved

Code execution is switched to a predefined exception handler function

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

We will show different error handling methods:

Basic use of exceptions

Create a custom exception handler

Multiple exceptions

Rethrow exception

Setting up top-level exception handler

The ultimate goal of throwing exceptions and catching exceptions of a specific type is to give corresponding solutions 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 abnormal
  5 */
 6 class HeavyParcelException extends Exception {}
 7 
 8 /**
  9 * Package Class
 10 */
11 class Parcel {
12 
13   /**
 14 * Parcel delivery destination address
 15 */
16   public $address;
17 
18   /**
 19 * Package weight
 20 */
21   public $weight;
22 }
23 
24 /**
 25 * Deliveryman
 26 */
27 class Courier {
28  
29   /**
 30 * Shipping
 31 */
32   public function ship(Parcel $parcel) {
33     //check we have an address
34     //If the destination of the package is empty35     if(empty($parcel->address)) {
36       throw new Exception('address not Specified(Address not filled in)!');
37     } 
38 
39     //check the weight
40     //If the weight exceeds 541     if($parcel->weight > 5) {
42       throw new HeavyParcelException('Parcel exceeds courier limit(The package exceeds the shipping limit)!');
43     } 
44 
45     //otherwise we're coll
46     return true;
47   }
48 }
49 
50 $myCourier = new Courier();
51 $parcel = new Parcel();
52 //add the address if we have it For testing, no address is filled in here53 $parcel->weight = 7;
54 try {
55   $myCourier->ship($parcel);
56   echo "parcel shipped";
57 } catch (HeavyParcelException $e) {//Catch HeavyParcelException without writing the type name of this exception, it runs to the normal Exception and throws it out58   echo "Parcel weight error(Weight Error): " . $e->getMessage();
59   //redirect them to choose another courier
60 } catch (Exception $e) {
61   echo "Someting went wrong(Error address): " . $e->getMessage();
62   //exit so we don't try to proceed any further
63   exit;
64 }
65 echo '<br/>';
66 $a = 123;
67 echo $a;

The order of code execution starting from line 54:

55 >

32 >

35 (The first check in the ship method is that the address is empty, and it will be thrown hereException, not 57 linesHeavyParcelException) >

60(CapturedException) >

616263 The output address is wrongexit;No line output 65 to 67 will be output

 Tips:

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

1. The order of capture should be viewedtryIn the code inthrowWhich type ofException, then watchcatchorder in.

Line 2.57 captures a specific typeHeavyParcelExceptionCan't write it wrong or write itExceptionThere will be problems. You can try it yourself.

1) For example, the address is not empty, line 57 is writtenHeavyParcelException111, will be caught in its parent object with 60 lines, weight error. This is not what we want.

2) For example, the address is empty, line 57 is writtenException, will throw an address error, but the capture is the one that is originally responsible for the weightcatch. This is not what we want either.

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.