1
0
mirror of https://github.com/php/doc-ru.git synced 2026-03-27 01:02:19 +01:00
Files
archived-doc-ru/reference/errorfunc/functions/set-exception-handler.xml
2021-04-23 09:54:21 +03:00

143 lines
5.3 KiB
XML
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.
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 5fabd07880ab15b0ad2cf7eb055c7c2b36d7120f Maintainer: rjhdby Status: ready -->
<!-- Reviewed: yes Maintainer: rjhdby -->
<!-- $Revision$ -->
<refentry xml:id="function.set-exception-handler" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>set_exception_handler</refname>
<refpurpose>
Задаёт пользовательский обработчик исключений
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>callable</type><methodname>set_exception_handler</methodname>
<methodparam><type>callable</type><parameter>exception_handler</parameter></methodparam>
</methodsynopsis>
<para>
Задаёт обработчик по умолчанию для случаев, когда исключение выброшено
вне блока try/catch. После вызова <parameter>exception_handler</parameter>
выполнение будет остановлено.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>exception_handler</parameter></term>
<listitem>
<para>
Имя функции, которая будет вызываться каждый раз, когда выбрасывается
неперехватываемое исключение. Функция-обработчик
должна принимать один аргумент - объект, представляющий выброшенное
исключение. До PHP 7 такая функция выглядит так:
</para>
<para>
<methodsynopsis>
<type>void</type><methodname><replaceable>handler</replaceable></methodname>
<methodparam><type>Exception</type><parameter>ex</parameter></methodparam>
</methodsynopsis>
</para>
<para>
В PHP 7 большинство внутренних ошибок теперь генерируют исключение, но класса
<classname>Error</classname>. Для них также будет вызываться указанный
обработчик исключений. Классы <classname>Error</classname> и
<classname>Exception</classname> реализуют интерфейс <classname>Throwable</classname>,
соответственно именно его следует использовать в сигнатуре функции-обработчика:
</para>
<para>
<methodsynopsis>
<type>void</type><methodname><replaceable>handler</replaceable></methodname>
<methodparam><type>Throwable</type><parameter>ex</parameter></methodparam>
</methodsynopsis>
</para>
<para>
В качестве этого аргумента можно передать &null;. В этом случае
обработчик вернётся к своему изначальному состоянию.
</para>
<caution>
<para>
Указание типа <classname>Exception</classname> для параметра
<parameter>ex</parameter> в вашей функции-обработчике приведёт
к проблемам в PHP 7 из-за изменённой иерархии классов исключений.
</para>
</caution>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Возвращает имя предыдущего заданного обработчика или &null; в случае
ошибки. Если предыдущих обработчиков определено не было, то также
возвращается &null;.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>
Пример использования <function>set_exception_handler</function>
</title>
<programlisting role="php">
<![CDATA[
<?php
function exception_handler($exception) {
echo "Неперехваченное исключение: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
throw new Exception('Неперехваченное исключение');
echo "Не выполнено\n";
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso"><!-- {{{ -->
&reftitle.seealso;
<para>
<simplelist>
<member><function>restore_exception_handler</function></member>
<member><function>restore_error_handler</function></member>
<member><function>error_reporting</function></member>
<member><link linkend="language.exceptions">Исключения PHP 5</link></member>
</simplelist>
</para>
</refsect1><!-- }}} -->
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->