11<?xml version =" 1.0" encoding =" utf-8" ?>
22<!-- $Revision$ -->
3- <!-- EN-Revision: 7a75b854c8c52226d38397e7e8177e339fdb273f Maintainer: hirokawa Status: ready -->
3+ <!-- EN-Revision: c81a48e58fc530a74827316027fae74668d17a1d Maintainer: hirokawa Status: ready -->
44<!-- CREDITS: takagi,mumumu -->
55
66<chapter xml : id =" language.exceptions" xmlns =" http://docbook.org/ns/docbook" >
9696 さらに、&finally; ブロックにも &return; 文が存在した場合は、
9797 &finally; ブロックから値が返されます。
9898 </para >
99+ <para >
100+ &try; ブロック内でスローされた例外と、
101+ &finally; ブロック内でスローされた例外には、
102+ もう一つ注意すべき相互作用があります。
103+ これら両方のブロックから例外がスローされると、
104+ &finally; ブロックからスローされた例外は伝播しますが、
105+ &try; ブロック内でスローされた例外は前の例外として使われます。
106+ </para >
99107 </sect1 >
100108
101109 <sect1 annotations =" chunk:false" xml : id =" language.exceptions.exception-handler" >
@@ -343,6 +351,12 @@ try {
343351?>
344352]]>
345353 </programlisting >
354+ &example.outputs;
355+ <screen >
356+ <![CDATA[
357+ A SpecificException was thrown, but we don't care about the details.
358+ ]]>
359+ </screen >
346360 </example >
347361 <example >
348362 <title >throw を 式として扱う</title >
@@ -357,6 +371,10 @@ function test() {
357371 do_something_risky() or throw new Exception('It did not work');
358372}
359373
374+ function do_something_risky() {
375+ return false; // Simulate failure
376+ }
377+
360378try {
361379 test();
362380} catch (Exception $e) {
@@ -365,6 +383,44 @@ try {
365383?>
366384]]>
367385 </programlisting >
386+ &example.outputs;
387+ <screen >
388+ <![CDATA[
389+ It did not work
390+ ]]>
391+ </screen >
392+ </example >
393+ <example >
394+ <title >try と finally の内部からスローされる例外</title >
395+ <programlisting role =" php" >
396+ <![CDATA[
397+ <?php
398+
399+ try {
400+ try {
401+ throw new Exception(message: 'Third', previous: new Exception('Fourth'));
402+ } finally {
403+ throw new Exception(message: 'First', previous: new Exception('Second'));
404+ }
405+ } catch (Exception $e) {
406+ var_dump(
407+ $e->getMessage(),
408+ $e->getPrevious()->getMessage(),
409+ $e->getPrevious()->getPrevious()->getMessage(),
410+ $e->getPrevious()->getPrevious()->getPrevious()->getMessage(),
411+ );
412+ }
413+ ]]>
414+ </programlisting >
415+ &example.outputs;
416+ <screen >
417+ <![CDATA[
418+ string(5) "First"
419+ string(6) "Second"
420+ string(5) "Third"
421+ string(6) "Fourth"
422+ ]]>
423+ </screen >
368424 </example >
369425 </sect1 >
370426
@@ -390,7 +446,7 @@ class Exception implements Throwable
390446 private $trace; // backtrace
391447 private $previous; // previous exception if nested exception
392448
393- public function __construct($message = '', $code = 0, Throwable $previous = null);
449+ public function __construct($message = '', $code = 0, ? Throwable $previous = null);
394450
395451 final private function __clone(); // Inhibits cloning of exceptions.
396452
@@ -437,7 +493,7 @@ class Exception implements Throwable
437493class MyException extends Exception
438494{
439495 // 例外を再定義し、メッセージをオプションではなくする
440- public function __construct($message, $code = 0, Throwable $previous = null) {
496+ public function __construct($message, $code = 0, ? Throwable $previous = null) {
441497 // なんらかのコード
442498
443499 // 全てを正しく確実に代入する
0 commit comments