Home > Backend Development > PHP Tutorial > Use examples to understand PHP5 exception handling_PHP tutorial

Use examples to understand PHP5 exception handling_PHP tutorial

WBOY
Release: 2016-07-15 13:32:25
Original
825 people have browsed it

Various errors will inevitably occur when writing a program, so how do I deal with these errors, or display the errors so that we can find changes? This uses exception handling. Here we will introduce to you the related concepts of 5 exception handling.

PHP5 exception handling

PHP 5 adds exception handling modules similar to other languages. Exceptions generated in PHP code can be thrown by the throw statement and caught by the catch statement. Code that requires exception handling must be placed in a try code block to catch possible exceptions.

Each try must have at least one corresponding catch. Use multiple catches to catch exceptions generated by different classes. When the try code block no longer throws an exception or no catch can be found to match the thrown exception, the PHP code will continue execution after jumping to the last catch.

Of course, PHP5 exception handling allows exceptions to be thrown again within catch blocks. When an exception is thrown, the subsequent code will not continue to execute, and PHP will try to find the first matching catch.

If an exception is not caught and there is no need to use set_exception_handler() for corresponding processing, then PHP will generate a serious error and output Uncaught Exception... (uncaught exception) prompt information.

Attributes and methods of the exception class of PHP5 exception handling

The following code is only to illustrate the structure of the built-in exception handling class. It is not a paragraph of practical significance. Available codes.

<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>
Copy after login

The above is the analysis of PHP5 exception handling. I hope it will be helpful to friends who need it again.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446145.htmlTechArticleIt is inevitable that various errors will occur when writing a program, so how do I deal with these errors? Errors are displayed so we can find changes? This uses exception handling. ...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template