用实例来理解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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

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 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
