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

Description of exceptions and errors in php

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "exception and error description in php". In daily operation, I believe that many people have doubts about exception and error description in php. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "exception and error description in php". Next, please follow the editor to study!

I. anomalies and errors

Exception refers to the situation that does not conform to the expected situation in the operation of the program and is different from the normal process. The error is its own problem, which is caused by an illegal syntax or environmental problem that prevents the compiler from running through the check settings.

Since php started out without exception handling, it later introduced exceptions in order to enter enterprise development and imitate languages such as java. Causing any self-error encountered in php will trigger an error instead of throwing an exception (in some cases, both an error and an exception will be thrown). When PHP encounters abnormal code, in most cases, it simply throws an error rather than an exception.

Php can use try...catch to catch exceptions only after you throw an exception (in general, some exceptions can be caught automatically).

Exceptions are typically used in the following scenarios in php:

1. Pessimistic prediction of the program: if you think your code can't handle all kinds of foreseeable and unpredictable situations one by one.

two。 The need of the program and the concern for the business: if the consistency of the data is very high, try...catch can be used to minimize the logical interruption caused by the exception, and after remedial processing, the integrity of the business logic will not be affected.

3. Language-level robustness requires: through precise control of the runtime process, when the program is interrupted, use try...catch to reduce the scope of possible errors, catch exceptions in time and make corresponding remedies.

Second, how to treat the abnormality of php

Due to historical reasons, the exception handling of php is insufficient. In most cases, it is impossible to automatically throw an exception. You must use if...else to judge before manually throwing an exception.

It doesn't make much sense to throw an exception manually, because it means that errors are fully anticipated in the code. At the same time, this approach will also make you confused in complex logical judgment and processing. Resulting in the loss of extraordinary real advantages.

So is there a better way to throw an exception? Yes, it's a combination error.

III. Errors in php

An error is a situation that makes the script run improperly.

The main error levels in php are as follows:

1. Deprecated: the lowest level of error, which means "not recommended, not recommended". For example, regular functions of the ereg family are used in php 5. Such errors are generally caused by the use of unrecommended and outdated functions or syntax. It does not affect the normal operation of the program, but it is recommended to correct it.

2. Notice: generally refers to the existence of inappropriate parts of grammar. This error will be reported if you use a variable but are not defined. Does not affect the normal process of the program.

3. Warning: a higher-level error that occurs only when there is a very inappropriate situation in the syntax, such as a mismatch of function parameters. Will result in not getting the expected results, and the code needs to be modified.

4. Fetal error: fatal error, which directly causes the program to stop running. This kind of error must be corrected.

5. Prase error: syntax parsing errors, all of which are run-time errors that are thrown before running.

There are a total of 16 error levels in php, but the main ones are the above.

Error.php

$data = '2012-12-20 regs if (ereg ("([0-9] {4})-([0-9] {1 data 2})-([0-9] {1 Invalid data format 2})", $data, $regs) {echo "$reg [3]. $regs [2]. $regs [1]";} else {echo "Invalid data format: $data";} $a = array (' o' = > 2,4,6,8); echo $ao; $result = array_sum ($a, 3); echo func () Echo 'will it be executed after a fatal error?' / / echo 'highest level error', $55

After the above code is executed, there will be four error levels. If you cannot fully see it, you need to modify the error level in your ini configuration file to E_ALL.

IV. Custom error handlers

You can use the set_error_handler () function to host the error handler, and you can customize the error handling flow.

If you want to unhost, you can use restore_error_handler () in the same page to unhost.

If you want to throw an error yourself, you can use the trigger_error () function.

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