Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to handle errors and exceptions in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to handle errors and exceptions in php. I hope you will get something after reading this article. Let's discuss it together.

In php, you can use various member functions built into the exception handling class "Exception" to obtain and return exception data, for example, the getMessage () function can return the message content of the exception; you can also catch exceptions in the program through "try catch" statements and "throw" keywords.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Exception handling classes in PHP

A built-in exception handling class, Exception, is provided in PHP. The common member functions in this class are as follows:

GetMessage (): returns the message content of the exception

GetCode (): returns the exception code as a number

GetFile (): returns the name of the file where the exception occurred

GetLine (): returns the line number where the error occurred

GetTrace (): returns the backtrace () array

GetTraceAsString (): returns the information that has been formatted into a string and generated by the function getTrace () function

_ _ toString (): generates abnormal string information, which can be overloaded. Notice that the foremost part of the function is two underscores.

The following code is the complete code for the Exception class, and from the definition of this class, you can see which properties and methods (member functions) are accessible and inherited in user-derived subclasses.

Exception {/ * attribute * / protected string $message; protected int $code; protected string $file; protected int $line / * method * / public _ _ construct ([string $message = "" [, int $code = 0 [ Throwable $previous = NULL]) final public getMessage (void): string final public getPrevious (void): Throwable final public getCode (void): int final public getFile (void): string final public getLine (void): int final public getTrace (void): array final public getTraceAsString (void): string public _ _ toString (void): string final private _ clone (void): void}

Catch exceptions in the program

To catch an exception in a program in PHP, you need to use the try catch statement and the throw keyword. Try catch statements are similar to process control statements, so you can implement an alternative conditional selection structure through try catch statements, while the throw keyword can throw an exception. The syntax format of the try catch statement is as follows:

Try {/ / Code that may have exceptions or errors, such as file operations, database operations, etc.} catch (Exception $e) {/ / $e is the object / / output error message of an exception class}

Code that requires exception handling must be placed in a block of try code in order to catch possible exceptions. Each try must have at least one corresponding catch. Using multiple catch, you can catch exceptions generated by different classes.

When the try code block no longer throws an exception or the catch can't be found to match the thrown exception, the PHP code continues to execute after jumping to the last catch.

Exceptions generated in PHP code can be thrown by throw statements and caught by catch statements. Of course, PHP allows (throw) exceptions to be thrown again within the catch code block.

When an exception is thrown, the subsequent code does not continue to execute, and PHP tries to continue to find the first catch that matches it. If an exception is not caught and it is not necessary to use set_exception_handler () to handle it, a serious error will be generated and a prompt for UncaughtException... (uncaught exception) will be output.

[example] use try catch and throw to catch exceptions in the program.

The running results are as follows:

Catch exception: throw exception message and jump out of try statement block error code: 12345 continue to execute code other than try catch statement

In the sample code, try to determine whether a directory named test exists under the current directory in the try statement block. If it does not exist, line 7 of the code is executed and an exception is thrown with the keyword throw. This exception is an object of the Exception class, generated by the new keyword, and initialized with the error message $err and error code 12345, so that you can get this information later when you catch the exception (line 11).

Once an exception is thrown, the remaining code in the try statement block will no longer be executed, the program flow will be transferred to the corresponding catch statement block for execution, and finally the error information and code will be output by calling its member functions through the Exception object.

After reading this article, I believe you have a certain understanding of "how to handle errors and exceptions in php". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report