Home php教程 php手册 php 错误处理与异常处理方法与实例教程

php 错误处理与异常处理方法与实例教程

May 25, 2016 pm 04:51 PM
deal with abnormal mistake

在程序开发中,错误处理这一块是非常重要的,今天本文章就来告诉他关于在php开发中,错误处理函数并且举例说明错误处理的重要性。

1、内置异常处理类、2、捕获并处理异常的示例、3、exception类的成员函数getmessage()、4、exception类的成员函数getfile()、5、exception类的成员函数getline()、6、显示警告或错误信息、7、自定义错误处理函数。

1,内置异常处理类

<?php
class exception {
    protected $message = &#39;unknown exception&#39;; // 异常信息
    protected $code = 0; // 用户自定义异常代码
    protected $file; // 发生异常的文件名
    protected $line; // 发生异常的代码行号
    function __construct($message = null, $code = 0);
    final function getmessage(); // 返回异常信息
    final function getcode(); // 返回异常代码
    final function getfile(); // 返回发生异常的文件名
    final function getline(); // 返回发生异常的代码行号
    final function gettrace(); // backtrace() 数组
    final function gettraceasstring(); // 已格成化成字符串的 gettrace() 信息
    function __tostring(); // 可输出的字符串
    
}
?>
Copy after login

2、捕获并处理异常的示例

<?php
try {
    $error = &#39;抛出异常信息,并且跳出try块<br/>&#39;;
    if (is_dir(&#39;./test&#39;)) {
        echo &#39;检测到../ch16是一个目录&#39;;
        echo &#39;<br/>&#39;;
        echo &#39;可能继续做其他一些操作&#39;;
        echo &#39;<br/>&#39;;
        echo &#39;....&#39;;
        echo &#39;<br/>&#39;;
    } else {
        throw new exception($error, 12345);
    }
    echo &#39;上面throw异常的话,这行代码不会执行,转而执行catch块<br/>&#39;;
}
catch(exception $e) {
    echo &#39;捕获异常: &#39; . $e->getmessage() . "<br/>错误代码:" . $e->getcode() . &#39;<br/>&#39;; //显示$error和123456
    echo &#39;<br/>&#39;;
}
echo &#39;继续执行&#39;;
?>
Copy after login

3、exception类的成员函数getmessage()

<?php
$file = &#39;./test/readme.txt&#39;;
try {
    if (is_dir($file)) {
        echo &#39;检测到目录&#39;;
    } else {
        //创建异常对象,错误信息将有exception类的成员函数getmessage()返回
        throw new exception(&#39;未找到该目录或文件&#39;);
    }
}
catch(exception $e) {
    echo &#39;捕获异常: &#39; . $e->getmessage();
    echo &#39;<br/>===============================&#39;;
    echo &#39;<br/>&#39;;
}
echo &#39;程序执行完毕&#39;;
?>
Copy after login

4、exception类的成员函数getfile()

<?php
$file = &#39;./test/readme.txt&#39;;
try {
    if (is_dir($file)) {
        echo &#39;检测到目录&#39;;
    } else {
        //创建异常对象,错误信息将有exception类的成员函数getmessage()返回
        throw new exception(&#39;未找到该目录或文件&#39;);
    }
}
catch(exception $e) {
    echo &#39;捕获异常: &#39; . $e->getmessage();
    echo &#39;<br/><br/>&#39;;
    echo &#39;错误所在文件:&#39; . $e->getfile();
    echo &#39;<br/>===============================&#39;;
    echo &#39;<br/>&#39;;
}
echo &#39;程序执行完毕&#39;;
?>
Copy after login

5、exception类的成员函数getline()

<?php
$file = &#39;./test/readme.txt&#39;;
try {
    if (is_dir($file)) {
        echo &#39;检测到目录&#39;;
    } else {
        //创建异常对象,错误信息将有exception类的成员函数getmessage()返回
        throw new exception(&#39;未找到该目录或文件&#39;);
    }
}
catch(exception $e) {
    echo &#39;捕获异常: &#39; . $e->getmessage();
    echo &#39;<br/><br/>&#39;;
    echo &#39;错误所在文件:&#39; . $e->getfile();
    echo &#39;<br/><br/>&#39;;
    echo &#39;错误所在行号:&#39; . $e->getline();
    echo &#39;<br/>===============================&#39;;
    echo &#39;<br/>&#39;;
}
echo &#39;程序执行完毕&#39;;
?>
Copy after login

6、显示警告或错误信息

<?php
error_reporting(e_warning | e_error); //显示警告和错误信息
echo $uvar; //这里产生注意信息,但不会显示出来
callfunc(); //这里产生一个致命错误,并且会显示出来

?>
Copy after login

7、自定义错误处理函数。

<?php
set_error_handler(&#39;myhandler&#39;); //自定义错误处理函数
function myhandler($code, $msg, $file, $line) {
    echo "<br/>";
    echo "程序 <b>$file</b> 执行过程中,在第 <b>$line</b> 行,产生一个错误。";
    echo "<br/>";
    echo "错误代码为:<b>$code</b>, 错误的原因是: <b>$msg</b>";
}
echo $uvar;
?>
Copy after login


本文链接:

收藏随意^^请保留教程地址.

Statement of this Website
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to complete operation (Error 0x0000771) Printer error Unable to complete operation (Error 0x0000771) Printer error Mar 16, 2024 pm 03:50 PM

If you encounter an error message when using your printer, such as the operation could not be completed (error 0x00000771), it may be because the printer has been disconnected. In this case, you can solve the problem through the following methods. In this article, we will discuss how to fix this issue on Windows 11/10 PC. The entire error message says: The operation could not be completed (error 0x0000771). The specified printer has been deleted. Fix 0x00000771 Printer Error on Windows PC To fix Printer Error the operation could not be completed (Error 0x0000771), the specified printer has been deleted on Windows 11/10 PC, follow this solution: Restart Print Spool

Solution to Windows Update prompt Error 0x8024401c error Solution to Windows Update prompt Error 0x8024401c error Jun 08, 2024 pm 12:18 PM

Table of Contents Solution 1 Solution 21. Delete the temporary files of Windows update 2. Repair damaged system files 3. View and modify registry entries 4. Turn off the network card IPv6 5. Run the WindowsUpdateTroubleshooter tool to repair 6. Turn off the firewall and other related anti-virus software. 7. Close the WidowsUpdate service. Solution 3 Solution 4 "0x8024401c" error occurs during Windows update on Huawei computers Symptom Problem Cause Solution Still not solved? Recently, the web server needs to be updated due to system vulnerabilities. After logging in to the server, the update prompts error code 0x8024401c. Solution 1

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Linux Oops: Detailed explanation of what this error means Linux Oops: Detailed explanation of what this error means Mar 21, 2024 am 09:06 AM

LinuxOops: Detailed explanation of the meaning of this error, need specific code examples What is LinuxOops? In Linux systems, &quot;Oops&quot; refers to a situation where a serious error in the kernel causes the system to crash. Oops is actually a kernel crash mechanism that stops the system when a fatal error occurs and prints out relevant error information so that developers can diagnose and fix the problem. Oops usually occur in kernel space and have nothing to do with user space applications. When the kernel encounters

MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection Jun 08, 2024 pm 06:09 PM

Today I would like to introduce to you an article published by MIT last week, using GPT-3.5-turbo to solve the problem of time series anomaly detection, and initially verifying the effectiveness of LLM in time series anomaly detection. There is no finetune in the whole process, and GPT-3.5-turbo is used directly for anomaly detection. The core of this article is how to convert time series into input that can be recognized by GPT-3.5-turbo, and how to design prompts or pipelines to let LLM solve the anomaly detection task. Let me introduce this work to you in detail. Image paper title: Largelanguagemodelscanbezero-shotanomalydete

Why does the Xiangxiangfuzhai app display an error? Why does the Xiangxiangfuzhai app display an error? Mar 19, 2024 am 08:04 AM

The display error is a problem that may occur in the Xiangxiang Fuzhai app. Some users are not sure why the Xiangxiang Fuzhai app displays errors. It may be due to network connection problems, too many background programs, incorrect registration information, etc. Next, This is the editor’s introduction to how to solve app display errors for users. Interested users should come and take a look! Why does the Xiangxiang Fuzhai app display an error answer: network connection problem, too many background programs, incorrect registration information, etc. Details: 1. [Network problem] Solution: Check the device connection network status, reconnect or choose another network connection to use. Can. 2. [Too many background programs] Solution: Close other running programs and release the system, which can speed up the running of the software. 3. [Incorrect registration information

A guide to the unusual missions in the Rise of Ronin Pool A guide to the unusual missions in the Rise of Ronin Pool Mar 26, 2024 pm 08:06 PM

The abnormality in the pool is a side task in the game. Many players want to know how to complete the abnormality in the pool task. It is actually very simple. First, we must master the technique of shooting in the water before we can accept the task and investigate the source of the stench. Later, we discovered It turns out that there are a lot of corpses under the pool. Let’s take a look at this graphic guide for the unusual tasks in the pool in Rise of Ronin. Guide to unusual missions in the Ronin Rise Pool: 1. Talk to Iizuka and learn the technique of shooting in the water. 2. Go to the location in the picture below to receive the abnormal task in the pool. 3. Go to the mission location and talk to the NPC, and learn that there is a foul smell in the nearby pool. 4. Go to the pool to investigate. 5. Swim to the location in the picture below, dive underwater, and you will find a lot of corpses. 6. Use a camera to take pictures of the corpse. 7

Interpreting Oracle error 3114: causes and solutions Interpreting Oracle error 3114: causes and solutions Mar 08, 2024 pm 03:42 PM

Title: Analysis of Oracle Error 3114: Causes and Solutions When using Oracle database, you often encounter various error codes, among which error 3114 is a relatively common one. This error generally involves database link problems, which may cause exceptions when accessing the database. This article will interpret Oracle error 3114, discuss its causes, and give specific methods to solve the error and related code examples. 1. Definition of error 3114 Oracle error 3114 pass

See all articles