xref: /web-php/manual/en/language.exceptions.php (revision c3f96532)
1<?php
2include_once __DIR__ . '/../../include/shared-manual.inc';
3$TOC = array();
4$PARENTS = array();
5include_once __DIR__ ."/toc/langref.inc";
6$setup = array (
7  'home' =>
8  array (
9    0 => 'index.php',
10    1 => 'PHP Manual',
11  ),
12  'head' =>
13  array (
14    0 => 'UTF-8',
15    1 => 'en',
16  ),
17  'this' =>
18  array (
19    0 => 'language.exceptions.php',
20    1 => 'Exceptions',
21  ),
22  'up' =>
23  array (
24    0 => 'langref.php',
25    1 => 'Language Reference',
26  ),
27  'prev' =>
28  array (
29    0 => 'language.namespaces.faq.php',
30    1 => 'FAQ: things you need to know about namespaces',
31  ),
32  'next' =>
33  array (
34    0 => 'language.exceptions.extending.php',
35    1 => 'Extending Exceptions',
36  ),
37  'alternatives' =>
38  array (
39  ),
40  'extra_header_links' =>
41  array (
42    'rel' => 'alternate',
43    'href' => '/manual/en/feeds/language.exceptions.atom',
44    'type' => 'application/atom+xml',
45  ),
46);
47$setup["toc"] = $TOC;
48$setup["parents"] = $PARENTS;
49manual_setup($setup);
50
51?>
52<div id="language.exceptions" class="chapter">
53  <h1>Exceptions</h1>
54<h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.exceptions.extending.php">Extending Exceptions</a></li></ul>
55
56
57  <p class="para">
58   PHP 5 has an exception model similar to that of other programming languages.
59   An exception can be <em>throw</em>n, and caught
60   (&quot;<em>catch</em>ed&quot;) within PHP. Code may be surrounded in a
61   <em>try</em> block, to facilitate the catching of potential
62   exceptions. Each <em>try</em> must have at least one
63   corresponding <em>catch</em> block. Multiple
64   <em>catch</em> blocks can be used to catch different classes of
65   exceptions. Normal execution (when no exception is thrown within the
66   <em>try</em> block, or when a <em>catch</em> matching
67   the thrown exception&#039;s class is not present) will continue after that last
68   <em>catch</em> block defined in sequence. Exceptions can be
69   <em>throw</em>n (or re-thrown) within a <em>catch</em> block.
70  </p>
71  <p class="para">
72   When an exception is thrown, code following the statement will not be
73   executed, and PHP will attempt to find the first matching
74   <em>catch</em> block. If an
75   exception is not caught, a PHP Fatal Error will be issued with an
76   &quot;<em>Uncaught Exception ...</em>&quot; message, unless a handler has
77   been defined with  <span class="function"><a href="function.set-exception-handler.php" class="function">set_exception_handler()</a></span>.
78  </p>
79  <p class="para">
80   In PHP 5.5 and later, a <em>finally</em> block may also be
81   specified after the <em>catch</em> blocks. Code within the
82   <em>finally</em> block will always be executed after the
83   <em>try</em> and <em>catch</em> blocks, regardless of
84   whether an exception has been thrown, and before normal execution resumes.
85  </p>
86  <p class="para">
87   The thrown object must be an instance of the <a href="class.exception.php" class="classname">Exception</a>
88   class or a subclass of <a href="class.exception.php" class="classname">Exception</a>. Trying to throw an
89   object that is not will result in a PHP Fatal Error.
90  </p>
91  <blockquote class="note"><p><strong class="note">Note</strong>:
92   <p class="para">
93    Internal PHP functions mainly use
94    <a href="errorfunc.configuration.php#ini.error-reporting" class="link">Error reporting</a>, only modern
95    <a href="language.oop5.php" class="link">Object oriented</a>
96    extensions use exceptions. However, errors can be simply translated to
97    exceptions with <a href="class.errorexception.php" class="link">ErrorException</a>.
98   </p>
99  </p></blockquote>
100  <div class="tip"><strong class="tip">Tip</strong>
101   <p class="para">
102    The <a href="intro.spl.php" class="link">Standard PHP Library (SPL)</a> provides a
103    good number of <a href="spl.exceptions.php" class="link">built-in exceptions</a>.
104   </p>
105  </div>
106   <div class="example" id="example-269">
107    <p><strong>Example #1 Throwing an Exception</strong></p>
108    <div class="example-contents">
109<div class="phpcode"><code><span style="color: #000000">
110<span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division&nbsp;by&nbsp;zero.'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">/</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br>}<br><br>try&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br>}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Caught&nbsp;exception:&nbsp;'</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$e</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: #FF8000">//&nbsp;Continue&nbsp;execution<br></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World\n"</span><span style="color: #007700">;<br></span><span style="color: #0000BB">?&gt;</span>
111</span>
112</code></div>
113    </div>
114
115    <div class="example-contents"><p>The above example will output:</p></div>
116    <div class="example-contents screen">
117<div class="cdata"><pre>
1180.2
119Caught exception: Division by zero.
120Hello World
121</pre></div>
122    </div>
123   </div>
124   <div class="example" id="example-270">
125    <p><strong>Example #2 Exception handling with a <em>finally</em> block</strong></p>
126    <div class="example-contents">
127<div class="phpcode"><code><span style="color: #000000">
128<span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division&nbsp;by&nbsp;zero.'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">/</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br>}<br><br>try&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br>}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Caught&nbsp;exception:&nbsp;'</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$e</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>}&nbsp;</span><span style="color: #0000BB">finally&nbsp;</span><span style="color: #007700">{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"First&nbsp;finally.\n"</span><span style="color: #007700">;<br>}<br><br>try&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br>}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Caught&nbsp;exception:&nbsp;'</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$e</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>}&nbsp;</span><span style="color: #0000BB">finally&nbsp;</span><span style="color: #007700">{<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Second&nbsp;finally.\n"</span><span style="color: #007700">;<br>}<br><br></span><span style="color: #FF8000">//&nbsp;Continue&nbsp;execution<br></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World\n"</span><span style="color: #007700">;<br></span><span style="color: #0000BB">?&gt;</span>
129</span>
130</code></div>
131    </div>
132
133    <div class="example-contents"><p>The above example will output:</p></div>
134    <div class="example-contents screen">
135<div class="cdata"><pre>
1360.2
137First finally.
138Caught exception: Division by zero.
139Second finally.
140Hello World
141</pre></div>
142    </div>
143   </div>
144   <div class="example" id="example-271">
145    <p><strong>Example #3 Nested Exception</strong></p>
146    <div class="example-contents">
147<div class="phpcode"><code><span style="color: #000000">
148<span style="color: #0000BB">&lt;?php<br><br></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyException&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Exception&nbsp;</span><span style="color: #007700">{&nbsp;}<br><br>class&nbsp;</span><span style="color: #0000BB">Test&nbsp;</span><span style="color: #007700">{<br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">testing</span><span style="color: #007700">()&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">MyException</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo!'</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">MyException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;rethrow&nbsp;it<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">throw&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Test</span><span style="color: #007700">;<br></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">testing</span><span style="color: #007700">();<br><br></span><span style="color: #0000BB">?&gt;</span>
149</span>
150</code></div>
151    </div>
152
153    <div class="example-contents"><p>The above example will output:</p></div>
154    <div class="example-contents screen">
155<div class="cdata"><pre>
156string(4) &quot;foo!&quot;
157</pre></div>
158    </div>
159   </div>
160
161
162
163 </div>
164<?php manual_footer(); ?>
165