Home  >  Article  >  Backend Development  >  What is the use of php exception handling?

What is the use of php exception handling?

烟雨青岚
烟雨青岚Original
2020-06-30 16:21:412289browse

Exception handling (Exception) is used to change the normal flow of the script when a specified error occurs. When an exception is thrown, the following code will not continue to execute, and PHP will try to find a matching "catch" code block.

What is the use of php exception handling?

Exception is used to change the normal flow of the script when a specified error occurs.

Basic use of exceptions

When an exception is thrown, the subsequent code will not continue to execute, PHP will try Find matching "catch" code blocks.

If the exception is not caught and there is no need to use set_exception_handler() for corresponding processing, then a serious error (fatal error) will occur and "Uncaught Exception" (uncaught exception) will be output. wrong information.

Let's try throwing an exception without catching it:

<?php
//create function with an exception
function checkNum($number)
{
if($number>1)
 {
 throw new Exception("Value must be 1 or below");
 }
return true;
}
//trigger exception
checkNum(2);
?>

The above code will get an error like this:

Fatal error: Uncaught exception &#39;Exception&#39; 
with message &#39;Value must be 1 or below&#39; in C:\webfolder\test.php:6 
Stack trace: #0 C:\webfolder\test.php(12): 
checkNum(28) #1 {main} thrown in C:\webfolder\test.php on line 6

Try, throw and catch

To avoid the errors in the above example, we need to create appropriate code to handle exceptions.

The correct handler should include:

Try - The function that uses the exception should be located within the "try" code block. If no exception is triggered, the code continues execution as usual. But if an exception is triggered, an exception will be thrown. Throw - This specifies how to trigger the exception. Each "throw" must correspond to at least one "catch"Catch - the "catch" code block will catch the exception and create an object containing the exception information

Let's trigger an exception:

<?php
//创建可抛出一个异常的函数
function checkNum($number)
{
if($number>1)
 {
 throw new Exception("Value must be 1 or below");
 }
return true;
}
//在 "try" 代码块中触发异常
try
{
checkNum(2);
//If the exception is thrown, this text will not be shown
echo &#39;If you see this, the number is 1 or below&#39;;
}
//捕获异常
catch(Exception $e)
{
echo &#39;Message: &#39; .$e->getMessage();
}
?>

The above code will get an error similar to this:

Message: Value must be 1 or below

Explanation of the example:

The above code An exception was thrown and caught:

Create the checkNum() function. It detects whether the number is greater than 1. If so, throw an exception. Call the checkNum() function in the "try" block. An exception is thrown in the checkNum() function. The "catch" code block receives the exception and creates an object ($e) containing the exception information. By calling $e->getMessage() from this exception object, the error message from the exception is output

However, in order to follow the principle of "each throw must correspond to a catch", a top-level exception can be set processor to handle missed errors.

Recommended tutorial: "php tutorial"

The above is the detailed content of What is the use of php exception handling?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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