svn: /phpdoc/ja/trunk/language/ exceptions.xml

From: Date: Thu, 26 Nov 2020 01:50:29 +0000
Subject: svn: /phpdoc/ja/trunk/language/ exceptions.xml
Groups: php.doc.ja 
Request: Send a blank email to doc-ja+get-5838@lists.php.net to get a copy of this message
mumumu Thu, 26 Nov 2020 01:50:29 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=351663 Log: sync with en Changed paths: U phpdoc/ja/trunk/language/exceptions.xml Modified: phpdoc/ja/trunk/language/exceptions.xml =================================================================== --- phpdoc/ja/trunk/language/exceptions.xml 2020-11-26 01:24:11 UTC (rev 351662) +++ phpdoc/ja/trunk/language/exceptions.xml 2020-11-26 01:50:29 UTC (rev 351663) @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: 351112 Maintainer: hirokawa Status: ready --> +<!-- EN-Revision: 351652 Maintainer: hirokawa Status: working --> <!-- CREDITS: takagi,mumumu --> <chapter xml:id="language.exceptions" xmlns="http://docbook.org/ns/docbook"> @@ -193,18 +193,41 @@ 各 &try; ブロックには、対応する &catch; ブロックあるいは &finally; ブロックが存在する必要があります。 </para> - + <!-- to be translated --> <para> + If an exception is thrown and its current function scope has no &catch; + block, the exception will "bubble up" the call stack to the calling + function until it finds a matching &catch; block. All &finally; blocks it encounters + along the way will be executed. If the call stack is unwound all the way to the + global scope without encountering a matching &catch; block, the program will + terminate with a fatal error unless a global exception handler has been set. + </para> + <para> スローされるオブジェクトは、<classname>Exception</classname> クラスあるいは <classname>Exception</classname> のサブクラスのインスタンスでなければなりません。 それ以外のオブジェクトをスローしようとすると PHP の Fatal Error が発生します。 </para> + <!-- to be translated --> + <para> + As of PHP 8.0.0, the &throw; keyword is an expression and may be used in any expression + context. In prior versions it was a statement and was required to be on its own line. + </para> + </simplesect> <simplesect xml:id="language.exceptions.catch"> <title><literal>catch</literal></title> + <!-- to be translated --> <para> + A &catch; block defines how to respond to a thrown exception. A &catch; + block defines one or more types of exception or error it can handle, and + optionally a variable to which to assign the exception. (The variable was + required prior to PHP 8.0.0.) The first &catch; block a thrown exception + or error encounters that matches the type of the thrown object will handle + the object. + </para> + <para> さまざまな型の例外を捕捉するために 複数の &catch; フロックを使用することができます。 通常の実行時 (&try; ブロック内で例外が投げられなかった @@ -212,6 +235,8 @@ ブロック内は処理されず、それ以降から処理が続けられます。 &catch; ブロックの中から例外を &throw; する (あるいは &throw; しなおす) こともできます。 + <!-- to be translated --> + If not, execution will continue after the &catch; block that was triggered. </para> <para> 例外が投げられた場合、その命令に続くコードは実行されず、 @@ -223,11 +248,17 @@ 定義されている場合を除きます。 </para> <para> - PHP 7.1 以降では、&catch; ブロック で 複数の例外を + PHP 7.1.0 以降では、&catch; ブロック で 複数の例外を パイプ文字 (<literal>|</literal>) を使って指定できるようになりました。 これは、異なるクラス改装からの例外を同時に扱う必要がある場合に有用です。 </para> + <!-- to be translted --> <para> + As of PHP 8.0.0, the variable name for a caught exception is optional. + If not specified, the &catch; block will still execute but will not + have access to the thrown object. + </para> + <para> &finally; ブロックと &return; 文の間には注意すべき相互作用があります。 &return; 文が &try; や &catch; ブロックの内部に存在した場合でも、 &finally; ブロックは実行されます。 @@ -248,7 +279,18 @@ </para> </simplesect> - <simplesect xml:id="language.exceptions.notes"> + <simplesect xml:id="language.exceptions.exception-handler"> + <title><literal>Global exception handler</literal></title> + <para> + If an exception is allowed to bubble up to the global scope, it may be caught + by a global exception handler if set. The <function>set_exception_handler</function> + function can set a function that will be called in place of a &catch; block if no + other block is invoked. The effect is essentially the same as if the entire program + were wrapped in a &try;-&catch; block with that function as the &catch;. + </para> + </simplesect> + + <simplesect xml:id="language.exceptions.notes"> &reftitle.notes; <note> @@ -260,7 +302,22 @@ の拡張モジュールのみです。 しかし、<link linkend="class.errorexception">ErrorException</link> を使えば簡単にエラーを例外に変換することができます。 + この変換テクニックが使えるのは、致命的でないエラーに限ります。 </para> + <example> + <title>エラーを例外に変換する</title> + <programlisting role="php"> +<![CDATA[ +<?php +function exceptions_error_handler($severity, $message, $filename, $lineno) { + throw new ErrorException($message, 0, $severity, $filename, $lineno); +} + +set_error_handler('exceptions_error_handler'); +?> +]]> + </programlisting> + </example> </note> <tip> <para> @@ -446,6 +503,51 @@ ]]> </screen> </example> + <!-- to be translated --> + <example> + <title>Omitting the caught variable</title> + <para>Only permitted in PHP 8.0.0 and later.</para> + <programlisting role="php"> +<![CDATA[ +<?php + +class SpecificException extends Exception {} + +function test() { + throw new SpecificException('Oopsie'); +} + +try { + test(); +} catch (SpecificException) { + print "A SpecificException was thrown, but we don't care about the details."; +} +?> +]]> + </programlisting> + </example> + <example> + <title>Throw as an expression</title> + <para>Only permitted in PHP 8.0.0 and later.</para> + <programlisting role="php"> +<![CDATA[ +<?php + +class SpecificException extends Exception {} + +function test() { + do_something_risky() or throw new Exception('It did not work'); +} + +try { + test(); +} catch (Exception $e) { + print $e->getMessage(); +} +?> +]]> + </programlisting> + </example> </simplesect> </chapter>

« previous php.doc.ja (#5838) next »