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.
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') ;
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( '现在有异常了');
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(); } ?>
The code demonstration result is as follows:
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(); }
The code demonstration results are as follows:
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 '因为懂得,所以宽容'; }
The code demonstration results are as follows:
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!