<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.errorfunc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.set-exception-handler.php',
    1 => 'set_exception_handler',
    2 => 'Sets a user-defined exception handler function',
  ),
  'up' => 
  array (
    0 => 'ref.errorfunc.php',
    1 => 'Error Handling Functions',
  ),
  'prev' => 
  array (
    0 => 'function.set-error-handler.php',
    1 => 'set_error_handler',
  ),
  'next' => 
  array (
    0 => 'function.trigger-error.php',
    1 => 'trigger_error',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/errorfunc/functions/set-exception-handler.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.set-exception-handler" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">set_exception_handler</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">set_exception_handler</span> &mdash; <span class="dc-title">
   Sets a user-defined exception handler function 
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.set-exception-handler-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>set_exception_handler</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span></span> <code class="parameter">$callback</code></span>): <span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span></span></div>

  <p class="para rdfs-comment">
   Sets the default exception handler if an exception is not caught within a
   try/catch block. Execution will stop after the
   <code class="parameter">callback</code> is called.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.set-exception-handler-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <p class="para">
       The function to be called when an uncaught exception occurs.
       This handler function needs to accept one parameter,
       which will be the <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span> object that was thrown.
       Both <span class="classname"><a href="class.error.php" class="classname">Error</a></span> and <span class="classname"><a href="class.exception.php" class="classname">Exception</a></span>
       implement the <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span> interface.
       This is the handler signature:
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">handler</span></span>(<span class="methodparam"><span class="type"><a href="class.throwable.php" class="type Throwable">Throwable</a></span> <code class="parameter">$ex</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

      </p>
      <p class="para">
       <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> may be passed instead, to reset this handler to its default state.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.set-exception-handler-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the previously defined exception handler, or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> on error. If
   no previous handler was defined, <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> is also returned.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.set-exception-handler-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>set_exception_handler()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">exception_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">Throwable $exception</span><span style="color: #007700">) {<br />  echo </span><span style="color: #DD0000">"Uncaught exception: " </span><span style="color: #007700">, </span><span style="color: #0000BB">$exception</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">set_exception_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">'exception_handler'</span><span style="color: #007700">);<br /><br />throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Uncaught Exception'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Not Executed\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.set-exception-handler-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.get-exception-handler.php" class="function" rel="rdfs-seeAlso">get_exception_handler()</a> - Gets the user-defined exception handler function</span></li>
    <li><span class="function"><a href="function.restore-exception-handler.php" class="function" rel="rdfs-seeAlso">restore_exception_handler()</a> - Restores the previously defined exception handler function</span></li>
    <li><span class="function"><a href="function.restore-error-handler.php" class="function" rel="rdfs-seeAlso">restore_error_handler()</a> - Restores the previous error handler function</span></li>
    <li><span class="function"><a href="function.error-reporting.php" class="function" rel="rdfs-seeAlso">error_reporting()</a> - Sets which PHP errors are reported</span></li>
    <li><a href="language.exceptions.php" class="link">Exceptions</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>