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 use try catch statement in php

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

Share

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

This article mainly shows you "how to use try catch sentences in php", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use try catch sentences in php" this article.

In php, try catch statements with throw keywords can be used to catch exceptions in the program, and exceptions generated in program code can be thrown by try throw statements and caught by catch statements; syntax "try {/ / Code} catch (Exception $e) {/ / output error}".

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

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.

These are all the contents of the article "how to use try catch sentences in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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