Home > Backend Development > PHP Problem > What is a custom exception handling class in PHP? How to solve custom exception handling class?

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

慕斯
Release: 2023-03-10 19:30:01
Original
1770 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:

<?php
function test($e)
{
echo $e->getMessage();
}
set_exception_handler(&#39;test&#39;);
throw new Exception( &#39;现在有异常了&#39;);
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:

<?php
class MyException extends Exception
{
function demo( )
{
  echo &#39;执行第二套方案<br />&#39; ;
}
}
try {
  echo &#39;我将于茫茫人海中访我唯一灵魂之伴侣<br />&#39;;
  throw new MyException( &#39;主人出错啦&#39;);
  echo &#39;得之,我幸;不得,我命<br />&#39;;
} 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 &#39;<hr>&#39;;
  $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 &#39;我将于茫茫人海中访我唯一灵魂之伴侣<br />&#39;;
  throw new MyException( &#39;主人出错啦&#39;);
  echo &#39;得之,我幸;不得,我命<br />&#39;;
} catch (MyException $e) {
  echo &#39;因为爱过,所以慈悲&#39;;
}catch (Exception $e){
  echo &#39;因为懂得,所以宽容&#39;;
}
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template