uTools-Manuals/docs/php/set_exception_handler.html
2019-04-28 19:00:34 +08:00

162 lines
7.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>设置用户自定义的异常处理函数</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.set-exception-handler" class="refentry">
<div class="refnamediv">
<h1 class="refname">set_exception_handler</h1>
<p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">set_exception_handler</span> &mdash; <span class="dc-title">
设置用户自定义的异常处理函数
</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.set-exception-handler-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>set_exception_handler</strong></span>
( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$exception_handler</code></span>
) : <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span></div>
<p class="para rdfs-comment">
设置默认的异常处理程序,用于没有用 try/catch 块来捕获的异常。
<code class="parameter">exception_handler</code> 调用后异常会中止。
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.set-exception-handler-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">exception_handler</code></dt>
<dd>
<p class="para">
当一个未捕获的异常发生时所调用函数的名称。
该处理函数需要接受一个参数,该参数是一个抛出的异常对象。
PHP 7 以前的异常处理程序签名:
</p>
<p class="para">
<div class="methodsynopsis dc-description">
<span class="methodname"><span class="replaceable">handler</span></span>
( <span class="methodparam"><span class="type"><a href="class.exception.html" class="type Exception">Exception</a></span> <code class="parameter">$ex</code></span>
) : <span class="type"><span class="type void">void</span></span></div>
</p>
<p class="para">
自 PHP 7 以来,大多数错误抛出 <strong class="classname">Error</strong> 异常,也能被捕获。
<strong class="classname">Error</strong>
<a href="class.exception.html" class="classname">Exception</a> 都实现了 <strong class="classname">Throwable</strong> 接口。
PHP 7 起,处理程序的签名:
</p>
<p class="para">
<div class="methodsynopsis dc-description">
<span class="methodname"><span class="replaceable">handler</span></span>
( <span class="methodparam"><span class="type"><span class="type Throwable">Throwable</span></span> <code class="parameter">$ex</code></span>
) : <span class="type"><span class="type void">void</span></span></div>
</p>
<p class="para">
也可以传递 <strong><code>NULL</code></strong> 值用于重置异常处理函数为默认值。
</p>
<div class="caution"><strong class="caution">Caution</strong>
<p class="para">
注意,如果在用户回调里将 <code class="parameter">ex</code> 参数的类型明确约束为<a href="class.exception.html" class="classname">Exception</a> PHP 7 中由于异常类型的变化,将会产生问题。
</p>
</div>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.set-exception-handler-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
返回之前定义的异常处理程序的名称,或者在错误时返回 <strong><code>NULL</code></strong>
如果之前没有定义错误处理程序,也会返回 <strong><code>NULL</code></strong>
</p>
</div>
<div class="refsect1 changelog" id="refsect1-function.set-exception-handler-changelog">
<h3 class="title">更新日志</h3>
<p class="para">
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>7.0.0</td>
<td>
传入 <code class="parameter">exception_handler</code> 的参数从 <a href="class.exception.html" class="classname">Exception</a> 改为 <strong class="classname">Throwable</strong>
</td>
</tr>
<tr>
<td>5.5.0</td>
<td>
之前版本里,如果传入 <strong><code>NULL</code></strong> ,函数会返回 <strong><code>TRUE</code></strong>
自 PHP 5.5.0 后,会返回上一次的异常处理器。
</td>
</tr>
</tbody>
</table>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.set-exception-handler-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-465">
<p><strong>Example #1 <span class="function"><strong>set_exception_handler()</strong></span> 范例</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">exception_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$exception</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Uncaught&nbsp;exception:&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$exception</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">set_exception_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">'exception_handler'</span><span style="color: #007700">);<br /><br />throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Uncaught&nbsp;Exception'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Not&nbsp;Executed\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.set-exception-handler-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="restore_exception_handler.html" class="function" rel="rdfs-seeAlso">restore_exception_handler()</a> - 恢复之前定义过的异常处理函数。</span></li>
<li class="member"><span class="function"><a href="restore_error_handler.html" class="function" rel="rdfs-seeAlso">restore_error_handler()</a> - 还原之前的错误处理函数</span></li>
<li class="member"><span class="function"><a href="error_reporting.html" class="function" rel="rdfs-seeAlso">error_reporting()</a> - 设置应该报告何种 PHP 错误</span></li>
<li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
<li class="member"><a href="language.exceptions.html" class="link">PHP 5 异常</a></li>
</ul>
</p>
</div>
</div></div></div></body></html>