PHP 自定义错误处理函数
在php开发中我们一般会使用php自带的错误处理方法来处理一些错误,但是有些我们需要自定义一些错误处理机制来解决系统自带不能解决的问题。
基本的错误处理:使用 die() 函数
第一个例子展示了一个打开文本文件的简单脚本:
代码如下 | 复制代码 |
$file=fopen("welcome.txt","r"); ?> |
如果文件不存在,您会获得类似这样的错误:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in C:webfoldertest.php on line 2为了避免用户获得类似上面的错误消息,我们在访问文件之前检测该文件是否存在:
代码如下 | 复制代码 |
if(!file_exists("welcome.txt")) |
现在,假如文件不存在,您会得到类似这样的错误消息:
File not found比起之前的代码,上面的代码更有效,这是由于它采用了一个简单的错误处理机制在错误之后终止了脚本。
不过,简单地终止脚本并不总是恰当的方式。让我们研究一下用于处理错误的备选的 PHP 函数。
下面我们来看一个自定错误处理函数
代码如下 | 复制代码 |
function myErrorHandler($errno, $errstr, $errfile, $errline){ function trigger_test($age){//抛出错误的测试函数
function myNtice($errno, $errstr, $errfile, $errline){ |
//如果要分别处理不同错误级别:
代码如下 | 复制代码 |
set_error_handler('myError',E_USER_ERROR); set_exception_handler('myWarning',E_USER_WARNING); set_exception_handler('myNtice',E_USER_NOTICE); |
trigger_error('故意抛出个错误,还是很严重的哪一种!',E_USER_ERROR);
下面还附上php一些错误代码详解
参数 | 描述 |
---|---|
error_level |
必需。为用户定义的错误规定错误报告级别。必须是一个值数。 参见下面的表格:错误报告级别。 |
error_message | 必需。为用户定义的错误规定错误消息。 |
error_file | 可选。规定错误在其中发生的文件名。 |
error_line | 可选。规定错误发生的行号。 |
error_context | 可选。规定一个数组,包含了当错误发生时在用的每个变量以及它们的值。 |
错误报告级别
这些错误报告级别是错误处理程序旨在处理的错误的不同的类型:
值 | 常量 | 描述 |
---|---|---|
2 | E_WARNING | 非致命的 run-time 错误。不暂停脚本执行。 |
8 | E_NOTICE |
Run-time 通知。 脚本发现可能有错误发生,但也可能在脚本正常运行时发生。 |
256 | E_USER_ERROR | 致命的用户生成的错误。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_ERROR。 |
512 | E_USER_WARNING | 非致命的用户生成的警告。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_WARNING。 |
1024 | E_USER_NOTICE | 用户生成的通知。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_NOTICE。 |
4096 | E_RECOVERABLE_ERROR | 可捕获的致命错误。类似 E_ERROR,但可被用户定义的处理程序捕获。(参见 set_error_handler()) |
8191 | E_ALL |
所有错误和警告,除级别 E_STRICT 以外。 (在 PHP 6.0,E_STRICT 是 E_ALL 的一部分) |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
