<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; 异常处理</title>
	<atom:link href="https://www.phppan.com/tag/%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>思考PHP语言三：异常处理</title>
		<link>https://www.phppan.com/2010/12/thinkinphp-3-exception/</link>
		<comments>https://www.phppan.com/2010/12/thinkinphp-3-exception/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 01:28:03 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[异常处理]]></category>
		<category><![CDATA[思考PHP语言]]></category>
		<category><![CDATA[断言]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1192</guid>
		<description><![CDATA[思考PHP语言三：异常处理 【概述】 异常处理是指在语言中能够使程序按照一种标准的方法对于某些运行时错误和其他 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>思考PHP语言三：异常处理<br />
<strong>【概述】</strong><br />
异常处理是指在语言中能够使程序按照一种标准的方法对于某些运行时错误和其他程序所检测到的异常事件做出反应。异常发生的时间是不可以确定的，如果一种语言不包括异常处理机制，这就会给语言带来额外的复杂性。<br />
一般来说，对于异常的处理有三种方案：<br />
1、将一个额外的参数作为状态变量，通过标记这个状态值判断是否发生了异常。在C的标准库中许多函数使用返回值作为出错的状态变量。<br />
2、将一个标号参数传给子程序，当异常发生时，通过标号将程序调用跳转到程序中的不同位置<br />
3、将一个异常处理独立出来，作为专门的子程序或类存在。</p>
<p>在基于C的语言中，将异常出现称之为thrown，而不是raise，是因为在标准的C程序库中已经存在一个叫做raise的函数。<br />
在PHP5中使用第3种方式。<br />
<strong>【PHP中的异常处理】</strong><br />
在PHP5以后，PHP添加了异常处理模块。PHP的异常处理模块与java有一些类似，异常可被 throw 语句抛出并被 catch 语句捕获。需要进行异常处理的代码都必须放入 try 代码块内，以便捕获可能存在的异常。每一个 try 至少要有一个与之对应的 catch。使用多个 catch 可以捕获不同的类所产生的异常(嗯，这里没有throws子句)。当 try 代码块不再抛出异常或者找不到 catch 能匹配所抛出的异常时，PHP 代码就会在跳转到最后一个 catch 的后面继续执行。所以在安排catch的顺序时，需要将详细或子类的异常类放到前面。在有一些情况下，不管try子句是否抛出异常，也不管是否捕获了异常，程序都要执行一个过程，此时我们就需要finally子句，只是在PHP中并没有finally子句（HOHO）。<br />
一个异常的简单示例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error msg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Caught exception: '</span><span style="color: #339933;">,</span>  <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>PHP提供了Exception类，以及在SPL中提供了一些内置的异常类，我们可以通过继承Exception类而实现属于自己的异常类。在一个面向对象系统的设计中我们需要考虑关于错误的处理情况，是以PHP的内部错误报告，还是以异常的方式给出，此时我们需要规划一套专属于我们系统的异常子系统。并且对于PHP的内部错误报告我们可以使用ErrorException进行转换，如下所示帮助文档中的代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> exception_error_handler<span style="color: #009900;">&#40;</span><span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errfile</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errline</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ErrorException<span style="color: #009900;">&#40;</span><span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errfile</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errline</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">set_error_handler</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;exception_error_handler&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Trigger exception */</span>
<span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>【断言】</strong><br />
断言常常被用于防错性的程序设计。在一个程序中可以有多条断言，以便确保程序计算的正确性。<br />
PHP提供了assert函数，当执行到此函数时，如果条件为真，则什么都不执行，如果条件为假，则警告并中断程序： Warning: assert() [function.assert]: Assertion failed<br />
经常我们会用echo或die来调试我们的程序，在调试完后我们需要删除相关调试代码。使用assert语句可以在出现问题时将程序调用，而不需要在后面将这些断言删除，这样可以节省删除操作并且在以后的维护过程中也可以重用这些语句，这里就有点测试的感觉了。<br />
当然，我们也可以将断言和异常一起使用，如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 断言回调函数，抛出异常
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> assert_callcack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;msg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set our assert options</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_ACTIVE<span style="color: #339933;">,</span>   <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_BAIL<span style="color: #339933;">,</span>     <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_WARNING<span style="color: #339933;">,</span>  <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 必须为true</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_CALLBACK<span style="color: #339933;">,</span> <span style="color: #0000ff;">'assert_callcack'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 触发一个断言*/</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Never reached'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>断言和异常的结合处在于断言的回调函数，只是需要注意的是：assert_options(ASSERT_WARNING,  true);<br />
因为断言的错误级别为warning。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/12/thinkinphp-3-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
