用实例来理解PHP5异常处理
一个程序的编写难免会出现各种各样的错误,那么我如何来处理这些错误,或将错误显示出来方便我们查找更改呢?这就用到了异常处理。这里我们将向大家介绍5异常处理的相关概念。
PHP5异常处理
PHP 5 添加了类似于其它语言的异常处理模块。在 PHP 代码中所产生的异常可被throw语句抛出并被catch语句捕获。需要进行异常处理的代码都必须放入 try 代码块内,以 便捕获可能存在的异常。
每一个 try 至少要有一个与之对应的 catch。使用多个 catch 可以捕获不同的类所产生的异常。当 try 代码块不再抛出异常或者找不到 catch 能匹配 所抛出的异常时,PHP 代码就会在跳转到最后一个 catch 的后面继续执行。
当然,PHP5异常处理允许在 catch 代码块内再次抛出(throw)异常。 当一个异常被抛出时,其后(译者注:指抛出异常时所在的代码块)的代码将不会继续 执行,而PHP 就会尝试查找第一个能与之匹配的 catch。
如果一个异常没有被捕获,而 且又没用使用 set_exception_handler() 作相应的处理的话,那么PHP将会产生一个严重的错误,并且输出 Uncaught Exception ... (未捕获异常)的提示信息。
PHP5异常处理的异常类的属性与方法
以下这段代码只为说明内置异常处理类的结构,它并不是一段有实际意义的可用代码。
<ol class="dp-xml"> <li class="alt"><span><span><?php </span></span></li> <li><span>class Exception{ </span></li> <li class="alt"> <span>protected $</span><span class="attribute">message</span><span> = </span><span class="attribute-value">'Unknown <br>exception'</span><span>; </span> </li> <li><span>// 异常信息 </span></li> <li class="alt"> <span>protected $</span><span class="attribute">code</span><span> = </span><span class="attribute-value">0</span><span>; </span> </li> <li><span>// 用户自定义异常代码 </span></li> <li class="alt"><span>protected $file; </span></li> <li><span>// 发生异常的文件名 </span></li> <li class="alt"><span>protected $line; </span></li> <li><span>// 发生异常的代码行号 </span></li> <li class="alt"> <span>function __construct($</span><span class="attribute">message</span><span> = <br></span><span class="attribute-value">null</span><span>, $</span><span class="attribute">code</span><span> = </span><span class="attribute-value">0</span><span>); </span> </li> <li><span>final function getMessage(); </span></li> <li class="alt"><span>// 返回异常信息 </span></li> <li><span>final function getCode(); </span></li> <li class="alt"><span>// 返回异常代码(代号) </span></li> <li><span>final function getFile(); </span></li> <li class="alt"><span> // 返回发生异常的文件名 </span></li> <li><span>final function getLine(); </span></li> <li class="alt"><span>// 返回发生异常的代码行号 </span></li> <li><span>final function getTrace(); </span></li> <li class="alt"><span>// backtrace() 数组 </span></li> <li><span>final function getTraceAsString(); </span></li> <li class="alt"><span>// 已格成化成字符串的 getTrace() 信息 </span></li> <li><span>//可重载的方法 </span></li> <li class="alt"><span>function __toString(); </span></li> <li><span>// 可输出的字符串 </span></li> <li class="alt"><span>} </span></li> <li><span>?> </span></li> <li class="alt"><span> </span></li> </ol>
以上就是对PHP5异常处理的分析,希望对又需要的朋友有所帮助。

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



The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

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

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

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.

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

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

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
