search
Homephp教程PHP源码PHP异常处理(Exception)什么情况下使用?

异常处理虽然说使用简单但是我们应该在哪些情况下使用异常处理呢,今天小编就来为各位介绍关于php异常处理的使用方法了,具体的操作细节如下所示。

<script>ec(2);</script>
首先看一段官方解释(异常处理),如上,其最后一句话说明解释异常与PHP错误的关联解决上。
下面来看异常与PHP错误机制各有什么优缺点?应该如何使用?
根据PHP官方说明PHP的错误机制(错误码)出现于异常之前,用于输入PHP的错误信息,从衍生性来看异常这个概念可以看成是自定义错误机制,用于弥补PHP错误机制(错误码)的不足。
这就说明了一点在信息量方便,异常, 可以包含更丰富的错误信息, 比如错误信息, 错误码, 错误的行数, 文件, 甚至出错上下文, 避免错误信息不丰富的问题
此次是性能上,对于异常处理的性能问题,这里附图说明,如下:
注: 图中有一处不严谨, 即在确定是否最后一个catch块的时候, 会同时判断(is_a), 如果是才进入最后一个catch块执行。
性能和方便, 往往是一个矛盾体,根据场景,自己来选择。
最后一个要讨论的是强迫症问题,如果所有错误都用异常,那么你会看到满屏的try{}catch{},错误很多,有自己决定怎么解决。这就是现在很多框架下会有自定义异常的扩展需求(太多可能的Uncaught Exception),我想意思可能是,交给用户自己设计。

注:使用try{}catch{}不能铺货WARNING错误,需要自己定义捕获错误的函数,例如:

set_error_handler(function () {
    throw new Exception('Ach!');
});
 
try {
    $result = 4 / 0;
} catch( Exception $e ){
    echo "Divide by zero, I don't fear you!".PHP_EOL;
    $result = 0;
}
 
restore_error_handler();

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software