xref: /PHP-8.1/ext/dom/tests/dom003.phpt (revision bd9f4fa6)
1--TEST--
2Test 3: Exception Test
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$dom = new domdocument;
9$dom->load(__DIR__."/book.xml");
10$rootNode = $dom->documentElement;
11print "--- Catch exception with try/catch\n";
12try {
13    $rootNode->appendChild($rootNode);
14} catch (domexception $e) {
15    ob_start();
16    var_dump($e);
17    $contents = ob_get_contents();
18    ob_end_clean();
19    echo preg_replace('/object\(DOMElement\).+\{.*?\}/s', 'DOMElement', $contents);
20}
21print "--- Don't catch exception with try/catch\n";
22$rootNode->appendChild($rootNode);
23
24
25?>
26--EXPECTF--
27--- Catch exception with try/catch
28object(DOMException)#%d (%d) {
29  ["message":protected]=>
30  string(23) "Hierarchy Request Error"
31  ["string":"Exception":private]=>
32  string(0) ""
33  ["code"]=>
34  int(3)
35  ["file":protected]=>
36  string(%d) "%sdom003.php"
37  ["line":protected]=>
38  int(8)
39  ["trace":"Exception":private]=>
40  array(1) {
41    [0]=>
42    array(6) {
43      ["file"]=>
44      string(%d) "%sdom003.php"
45      ["line"]=>
46      int(8)
47      ["function"]=>
48      string(11) "appendChild"
49      ["class"]=>
50      string(7) "DOMNode"
51      ["type"]=>
52      string(2) "->"
53      ["args"]=>
54      array(1) {
55        [0]=>
56        DOMElement
57      }
58    }
59  }
60  ["previous":"Exception":private]=>
61  NULL
62}
63--- Don't catch exception with try/catch
64
65Fatal error: Uncaught DOMException: Hierarchy Request Error in %sdom003.php:%d
66Stack trace:
67#0 %sdom003.php(%d): DOMNode->appendChild(Object(DOMElement))
68#1 {main}
69  thrown in %sdom003.php on line %d
70