<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.error-handling.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ru',
  ),
  'this' => 
  array (
    0 => 'language.error-handling.throwable.php',
    1 => 'Throwable errors',
    2 => 'Throwable errors',
  ),
  'up' => 
  array (
    0 => 'language.error-handling.php',
    1 => 'Error Handling',
  ),
  'prev' => 
  array (
    0 => 'language.error-handling.php',
    1 => 'Error Handling',
  ),
  'next' => 
  array (
    0 => 'language.error-handling.diagnostic.php',
    1 => 'Diagnostic errors',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/error-handling.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.error-handling.throwable" class="sect1">
  <h2 class="title"><span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> errors</h2>
  
  <p class="simpara">
   A <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error is an execution event which disrupts
   the normal flow of instructions. When a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error
   is <a href="" class="link"><code class="literal">throw</code></a>n, be that by the engine or manually, it will unwind the call stack
   until it is caught by a <a href="" class="link"><code class="literal">catch</code></a> block or the global scope is reached, in which
   case the program is terminated with a fatal error.
   All <a href="" class="link"><code class="literal">finally</code></a> blocks it encounters along the way will be executed.
  </p>
  <p class="simpara">
   To throw a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error from within PHP code, one
   must use the <a href="" class="link"><code class="literal">throw</code></a> keyword followed by an instance of the
   <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span> or <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span> class or
   a subclass of one of them.
   Trying to throw an object that is not will result in an
   <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span> being thrown with the following message:
   <code class="literal">Cannot throw objects that do not implement Throwable</code>.
  </p>
  <div class="tip"><strong class="tip">Подсказка</strong>
   <p class="simpara">
    The <a href="book.spl.php#intro.spl" class="link">Standard PHP Library (SPL)</a> provides
    various <a href="spl.exceptions.php" class="link">built-in exceptions</a>.
    And it&#039;s possible to create custom exceptions by
    <a href="language.error-handling.throwable.php#language.error-handling.throwable.extending" class="link">extending the
    Exception class</a>.
   </p>
  </div>
  <p class="simpara">
   It is possible to handle a <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> error within PHP
   by surrounding the code susceptible to throw an error in a <a href="" class="link"><code class="literal">try</code></a> block.
   A <a href="" class="link"><code class="literal">try</code></a> block must have at least one corresponding <a href="" class="link"><code class="literal">catch</code></a> or <a href="" class="link"><code class="literal">finally</code></a> block.
  </p>
  <blockquote class="note"><p><strong class="note">Замечание</strong>: 
   <p class="simpara">
    It is <em>not</em> recommended to catch <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span>
    objects as those signal a programming error.
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">Внимание</strong>
   <p class="simpara">
    <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> objects cannot be cloned.
    Attempting to <a href="language.oop5.cloning.php" class="link">clone</a> such an
    object throws an <span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span>, even if a subclass declares
    its own <a href="language.oop5.cloning.php#object.clone" class="link">__clone()</a> method, with
    the following message:
    <code class="literal">Trying to clone an uncloneable object of class Exception</code>.
   </p>
  </div>

  <div class="sect2" id="language.error-handling.throwable.user">
   <h3 class="title"><code class="literal">Global exception handler</code></h3>
   <p class="simpara">
    It&#039;s possible to add a handler for any uncaught
    <span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> with
    <span class="function"><a href="function.set-exception-handler.php" class="function">set_exception_handler()</a></span>.
    This is similar to wrapping the entire code in a <a href="" class="link"><code class="literal">try</code></a>-<a href="" class="link"><code class="literal">catch</code></a> block,
    except that execution does not resume afterwards: the script
    terminates once the handler returns.
   </p>
  </div>

  <div class="sect2" id="language.error-handling.throwable.extending">
   <h3 class="title">Extending Exceptions</h3>
   <p class="simpara">
    A user-defined exception can be defined by extending the built-in
    <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span> class.
    To see which methods and properties are accessible within a child class
    derived from <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span> visit its page.
   </p>
   <p class="simpara">
    If a class extends the built-in <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span> class and
    re-defines the <a href="language.oop5.decon.php" class="link">constructor</a>,
    it is highly recommended that it also call <code class="code">parent::__construct()</code>
    to ensure all available data has been properly assigned.
    The <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a> method can be overridden
    to provide a custom output when the object is used as a string.
   </p>
   <div class="example" id="example-1">
    <p><strong>Пример #1 Extending <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">class </span><span style="color: #0000BB">CustomException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception
</span><span style="color: #007700">{
    </span><span style="color: #FF8000">// Redefine the constructor so message isn't optional
    </span><span style="color: #007700">public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">$code </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">, ?</span><span style="color: #0000BB">Throwable $previous </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">) {
        </span><span style="color: #FF8000">// some code

        // Ensure everything is assigned properly
        </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">$code</span><span style="color: #007700">, </span><span style="color: #0000BB">$previous</span><span style="color: #007700">);
    }

    </span><span style="color: #FF8000">// Custom string representation of object
    </span><span style="color: #007700">public function </span><span style="color: #0000BB">__toString</span><span style="color: #007700">() {
        return </span><span style="color: #0000BB">__CLASS__ </span><span style="color: #007700">. </span><span style="color: #DD0000">": [</span><span style="color: #007700">{</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">code</span><span style="color: #007700">}</span><span style="color: #DD0000">]: </span><span style="color: #007700">{</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">message</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;
    }

    public function </span><span style="color: #0000BB">customFunction</span><span style="color: #007700">() {
        echo </span><span style="color: #DD0000">"A custom function for this type of exception\n"</span><span style="color: #007700">;
    }
}

try {
    throw new </span><span style="color: #0000BB">CustomException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Something happened"</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);
} catch (</span><span style="color: #0000BB">CustomException $e</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">"Caught custom exception\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">;
    </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">customFunction</span><span style="color: #007700">();
}</span></code></pre></div>
    </div>

    
<div class="example-contents"><p>
 Результат выполнения приведённого примера:
</p></div>

    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Caught custom exception
CustomException: [5]: Something happened
A custom function for this type of exception
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="language.error-handling.throwable.hierarchy">
   <h3 class="title"><span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span> hierarchy</h3>

   <ul class="itemizedlist">
    <li class="listitem">
     <p class="simpara"><span class="exceptionname"><a href="class.throwable.php" class="exceptionname">Throwable</a></span></p>
     <ul class="itemizedlist">
      <li class="listitem">
       <p class="simpara"><span class="exceptionname"><a href="class.error.php" class="exceptionname">Error</a></span></p>
       <ul class="itemizedlist">
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.arithmeticerror.php" class="exceptionname">ArithmeticError</a></span></p>
         <ul class="itemizedlist">
          <li class="listitem">
           <p class="simpara"><span class="exceptionname"><a href="class.divisionbyzeroerror.php" class="exceptionname">DivisionByZeroError</a></span></p>
          </li>
         </ul>
        </li>
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.assertionerror.php" class="exceptionname">AssertionError</a></span></p>
        </li>
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.compileerror.php" class="exceptionname">CompileError</a></span></p>
         <ul class="itemizedlist">
          <li class="listitem">
           <p class="simpara"><span class="exceptionname"><a href="class.parseerror.php" class="exceptionname">ParseError</a></span></p>
          </li>
         </ul>
        </li>
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span></p>
         <ul class="itemizedlist">
          <li class="listitem">
           <p class="simpara"><span class="exceptionname"><a href="class.argumentcounterror.php" class="exceptionname">ArgumentCountError</a></span></p>
          </li>
         </ul>
        </li>
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span></p>
        </li>
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.unhandledmatcherror.php" class="exceptionname">UnhandledMatchError</a></span></p>
        </li>
        <li class="listitem">
         <p class="simpara"><span class="exceptionname"><a href="class.fibererror.php" class="exceptionname">FiberError</a></span></p>
        </li>
       </ul>
      </li>
      <li class="listitem">
       <p class="simpara"><span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span></p>
      </li>
     </ul>
    </li>
   </ul>

   <p class="simpara">
    The <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span> class has many subclasses,
    among them the <a href="spl.exceptions.php" class="link">SPL exceptions</a> and
    those defined by extensions.
   </p>
  </div>
 </div><?php manual_footer($setup); ?>