What is a custom exception handling class in PHP? How to solve custom exception handling class?

慕斯
Release: 2023-03-10 19:30:01
Original
1651 people have browsed it

The previous article introduced you to "What is exception handling in PHP? How to use try-catch in exception handling?》, this article continues to introduce to you what is a custom exception handling class in PHP? How can we solve custom exception handling class? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is a custom exception handling class in PHP? How to solve custom exception handling class?

Custom exception handling class:

final:If used to modify the class, then it represents this The class cannot be inherited. If it is used to modify a method, it means that this method cannot be overridden.

To inherit from the official exception handling class, the method can be added by yourself. Pay attention to whether the parent class method can be overridden.

[Note] If there are multiple catches, write the custom exception class above and the official exception class below

Nesting:You can create a try in try

Custom exception handling function (understand)

set_exception_ handler('test') ;
Copy after login

Register a function. When an exception is thrown, it will be automatically caught by this function. , = This function has one parameter, and the parameter is the exception object

Let’s take the code as an example:

getMessage(); } set_exception_handler('test'); throw new Exception( '现在有异常了');
Copy after login

What is a custom exception handling class in PHP? How to solve custom exception handling class?

Regarding the exception handling class, we still use the code Let me explain the form to you. First, we still need to create a new file. We define a class. We need to inherit the official exception handling class. Then we define a (function) method in the class. Suppose we try to execute the code through try. At that time, we can catch it through our own exception handling class. We demonstrate it through the code as follows:

' ; } } try { echo '我将于茫茫人海中访我唯一灵魂之伴侣
'; throw new MyException( '主人出错啦'); echo '得之,我幸;不得,我命
'; } catch (MyException $e) { echo $e->getMessage(); } ?>
Copy after login

The code demonstration result is as follows:

What is a custom exception handling class in PHP? How to solve custom exception handling class?

The following is our own defined exception handling class. If there is an error in execution, we can execute the second set of methods.

catch (MyException $e) { echo $e->getMessage(); echo '
'; $e->demo(); }
Copy after login

The code demonstration results are as follows:

What is a custom exception handling class in PHP? How to solve custom exception handling class?

This is the exception handling class we defined ourselves;

Let’s copy the code just now, and let’s find out which catch can catch the exception. The code is as follows:

try { echo '我将于茫茫人海中访我唯一灵魂之伴侣
'; throw new MyException( '主人出错啦'); echo '得之,我幸;不得,我命
'; } catch (MyException $e) { echo '因为爱过,所以慈悲'; }catch (Exception $e){ echo '因为懂得,所以宽容'; }
Copy after login

The code demonstration results are as follows:

What is a custom exception handling class in PHP? How to solve custom exception handling class?

It can be seen from the code that the exception captured is our custom exception handling class to capture this object. If we combine MyException and Exception What would happen if the order was changed? When we run it, we will find that it is still the first one,

Note:Exception is an official class, it is a parent class, it is a parent class of MyException, if they are both at the same time When catching exceptions, we need to put the subclass first, and then write the official class.

Recommended learning:php video tutorial

The above is the detailed content of What is a custom exception handling class in PHP? How to solve custom exception handling class?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!