<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/langref.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.exceptions.php',
    1 => 'Les exceptions',
    2 => 'Les exceptions',
  ),
  'up' => 
  array (
    0 => 'langref.php',
    1 => 'R&eacute;f&eacute;rence du langage',
  ),
  'prev' => 
  array (
    0 => 'language.errors.php7.php',
    1 => 'Les Erreurs en PHP 7',
  ),
  'next' => 
  array (
    0 => 'language.exceptions.extending.php',
    1 => '&Eacute;tendre les Exceptions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/exceptions.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/language.exceptions.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.exceptions" class="chapter">
 <h1 class="title">Les exceptions</h1>
<h2>Sommaire</h2><ul class="chunklist chunklist_chapter"><li><a href="language.exceptions.extending.php">&Eacute;tendre les Exceptions</a></li></ul>

 <p class="para">
  PHP a une gestion des exceptions similaire à ce qu&#039;offrent les autres
  langages de programmation.
  Une exception peut être lancée (&quot;<a href="language.exceptions.php" class="link"><code class="literal">throw</code></a>&quot;) et attrapée
  (&quot;<a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>&quot;) dans PHP. Le code devra être entouré
  d&#039;un bloc <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> pour faciliter la saisie d&#039;une exception
  potentielle. Chaque <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> doit avoir au moins
  un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> ou <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> correspondant.
 </p>
 <p class="para">
  Si une exception est lancée et que la portée courante de la fonction n&#039;a pas
  de bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>, l&#039;exception &quot;remontera&quot; la pile d&#039;appel de la fonction appelante
  jusqu&#039;à trouver un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> correspondant. Tous les blocs <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> rencontrés
  seront exécutés. Si la pile d&#039;appel est déroulée jusqu&#039;à la portée globale sans
  rencontrer de bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> correspondant, le programme sera terminé avec
  une erreur fatale sauf si un gestionnaire global d&#039;exception a été défini.
 </p>
 <p class="para">
  L&#039;objet lancé doit être une <a href="language.operators.type.php" class="link"><code class="literal">instanceof</code></a> <span class="interfacename"><a href="class.throwable.php" class="interfacename">Throwable</a></span>.
  Tenter de lancer un objet qui ne l&#039;est pas résultera en une erreur fatale émise par PHP.
 </p>
 <p class="para">
  À partir de PHP 8.0.0, le mot clé <a href="language.exceptions.php" class="link"><code class="literal">throw</code></a> est une expression et peut être
  utilisé dans n&#039;importe quel contexte d&#039;expressions. Dans les versions
  antérieures c&#039;était une déclaration qui devait être sur sa propre ligne.
 </p>

  <div id="language.exceptions.catch" class="sect1">
   <h2 class="title"><code class="literal">catch</code></h2>
   <p class="para">
    Un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> définit comment réagir à une exception qui a été lancée.
    Un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> définit un ou plusieurs types d&#039;exceptions ou erreurs qu&#039;il peut
    gérer, et optionnellement une variable dans laquelle assigner l&#039;exception.
    (Cette variable était requise dans les versions antérieures à PHP 8.0.0)
    Le premier bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> qu&#039;une exception ou erreur lancée rencontre et qui correspond au
    type de l&#039;objet lancé gérera l&#039;objet.
   </p>
   <p class="para">
    Plusieurs blocs <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> peuvent être utilisés pour attraper différentes
    classes d&#039;exceptions. L&#039;exécution normale (lorsqu&#039;aucune exception n&#039;est lancée
    dans le bloc <a href="language.exceptions.php" class="link"><code class="literal">try</code></a>) continue après le dernier
    bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> défini dans la séquence. Les exceptions
    peuvent être lancées (<a href="language.exceptions.php" class="link"><code class="literal">throw</code></a>) ou relancées dans un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>. Sinon,
    l&#039;exécution continuera après le bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> qui a été déclenché.
   </p>
   <p class="para">
    Lorsqu&#039;une exception est lancée, le code suivant le traitement ne sera pas
    exécuté et PHP tentera de trouver le premier bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> correspondant.
    Si une exception n&#039;est pas attrapée, une erreur fatale issue de PHP sera
    envoyée avec un message &quot;<code class="literal">Uncaught Exception ...</code>&quot;
    indiquant que l&#039;exception n&#039;a pu être attrapée à moins qu&#039;un gestionnaire
    d&#039;exceptions ne soit défini avec la fonction
    <span class="function"><a href="function.set-exception-handler.php" class="function">set_exception_handler()</a></span>.
   </p>
   <p class="para">
    À partir de PHP 7.1, un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> peut spécifier plusieurs exceptions à
    l&#039;aide du caractère pipe (<code class="literal">|</code>). Ceci est utile lorsque
    différentes exceptions de hiérarchies de classes différentes sont traitées
    de la même manière.
   </p>
   <p class="para">
    À partir de PHP 8.0.0, le nom de variable pour l&#039;exception attrapée est
    optionnel. Si non spécifié, le bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> sera toujours exécuté mais
    n&#039;aura pas accès à l&#039;objet lancé.
   </p>
  </div>

  <div id="language.exceptions.finally" class="sect1">
   <h2 class="title"><code class="literal">finally</code></h2>
   <p class="para">
    Un bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> peut aussi être
    spécifié après ou à la place des blocs <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>. Le code à l&#039;intérieur
    du bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> sera toujours exécuté après les blocs
    <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> et <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>, indépendamment du fait qu&#039;une
    exception a été lancée, avant de continuer l&#039;exécution normale.
   </p>
   <p class="para">
    Une interaction notable est entre un bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> et une déclaration
    <a href="function.return.php" class="link"><code class="literal">return</code></a>.
    Si une déclaration <a href="function.return.php" class="link"><code class="literal">return</code></a> est rencontrée à l&#039;intérieur des blocs <a href="language.exceptions.php" class="link"><code class="literal">try</code></a>
    ou <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>, le bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> sera quand même exécuté. De plus,
    la déclaration <a href="function.return.php" class="link"><code class="literal">return</code></a> est évaluée quand elle est rencontrée, mais le
    résultat sera retourné après que le bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> soit exécuté.
    Additionnellement, si le bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> contient aussi une déclaration
    <a href="function.return.php" class="link"><code class="literal">return</code></a> la valeur du bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> est retournée.
   </p>
   <p class="para">
    Une autre interaction notable est entre une exception lancée dans un bloc <a href="language.exceptions.php" class="link"><code class="literal">try</code></a>,
    et une exception lancée dans un bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a>. Si une exception est lancée dans les deux blocs,
    alors, l&#039;exception lancée dans le bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> sera celle qui sera propagée,
    et l&#039;exception lancée dans le bloc <a href="language.exceptions.php" class="link"><code class="literal">try</code></a> sera utilisée comme exception précédente.
   </p>
  </div>

 <div id="language.exceptions.exception-handler" class="sect1">
  <h2 class="title">Gestionnaire d&#039;exceptions global</h2>
  <p class="para">
   Si une exception est autorisée à remonter jusqu&#039;à la portée globale, elle peut
   être attrapée par un gestionnaire d&#039;exceptions global s&#039;il a été défini. La fonction
   <span class="function"><a href="function.set-exception-handler.php" class="function">set_exception_handler()</a></span> peut définir une fonction qui sera
   appelée à la place d&#039;un bloc <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> si aucun autre bloc n&#039;est invoqué.
   L&#039;effet est essentiellement identique à entourer le programme entier dans un
   bloc <a href="language.exceptions.php" class="link"><code class="literal">try</code></a>-<a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a> avec cette fonction en tant que <a href="language.exceptions.php#language.exceptions.catch" class="link"><code class="literal">catch</code></a>.
  </p>
 </div>

 <div id="language.exceptions.notes" class="sect1">
   <h2 class="title">Notes</h2>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Les fonctions internes de PHP utilisent principalement l&#039;
     <a href="errorfunc.configuration.php#ini.error-reporting" class="link">Error reporting</a>, seules les extensions
     <a href="language.oop5.php" class="link">orientées objet</a> modernes
     utilisent les exceptions. Quoi qu&#039;il en soit, des erreurs peuvent facilement être traduites en
     exceptions avec <a href="class.errorexception.php" class="link">ErrorException</a>.
     Cependant, cette technique ne fonctionne que pour les erreurs non fatales.
    </p>
    <div class="example" id="example-1">
     <p><strong>Exemple #1 Convertir l&#039;error reporting en exceptions</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">exceptions_error_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$severity</span><span style="color: #007700">, </span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$lineno</span><span style="color: #007700">) {<br />    throw new </span><span style="color: #0000BB">ErrorException</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$severity</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$lineno</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">set_error_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">'exceptions_error_handler'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p></blockquote>
   <div class="tip"><strong class="tip">Astuce</strong>
    <p class="para">
     La <a href="book.spl.php#intro.spl" class="link">bibliothèque standard PHP (SPL)</a> fournit
     un bon nombre <a href="spl.exceptions.php" class="link">d&#039;exceptions additionnelles</a>.
    </p>
   </div>
 </div>

  <div id="language.exceptions.examples" class="sect1">
   <h2 class="title">Exemples</h2>

   <div class="example" id="example-2">
    <p><strong>Exemple #2 Lancer une exception</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">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />    if (!</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division par zéro.'</span><span style="color: #007700">);<br />    }<br />    return </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 {<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Exception reçue : '</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">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// On continue l'exécution<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Bonjour le monde !\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0.2
Exception reçue : Division par zéro.
Bonjour le monde !
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Gestion de l&#039;exception avec un bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a></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">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />    if (!</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division par zéro.'</span><span style="color: #007700">);<br />    }<br />    return </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 {<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Exception reçue : '</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">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} finally {<br />    echo </span><span style="color: #DD0000">"Première fin.\n"</span><span style="color: #007700">;<br />}<br /><br />try {<br />    echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Exception reçue : '</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">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} finally {<br />    echo </span><span style="color: #DD0000">"Seconde fin.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// On continue l'exécution<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Bonjour le monde !\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0.2
Première fin.
Exception reçue : Division par zéro.
Seconde fin.
Bonjour le monde !
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>Exemple #4 Interaction entre le bloc <a href="language.exceptions.php#language.exceptions.finally" class="link"><code class="literal">finally</code></a> et <a href="function.return.php" class="link"><code class="literal">return</code></a></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br />    try {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br />    } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />        return </span><span style="color: #DD0000">'catch'</span><span style="color: #007700">;<br />    } finally {<br />        return </span><span style="color: #DD0000">'finally'</span><span style="color: #007700">;<br />    }<br />}<br /><br />echo </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
finally
</pre></div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>Exemple #5 Héritage d&#039;une exception</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">Test </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">testing</span><span style="color: #007700">() {<br />        try {<br />            try {<br />                throw new </span><span style="color: #0000BB">MyException</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo!'</span><span style="color: #007700">);<br />            } catch (</span><span style="color: #0000BB">MyException $e</span><span style="color: #007700">) {<br />                </span><span style="color: #FF8000">// on la relance<br />                </span><span style="color: #007700">throw </span><span style="color: #0000BB">$e</span><span style="color: #007700">;<br />            }<br />        } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />            </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 />        }<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </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></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(4) &quot;foo!&quot;
</pre></div>
    </div>
   </div>
   <div class="example" id="example-6">
    <p><strong>Exemple #6 Gestion des exceptions de capture multiple</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">MyOtherException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">Test </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">testing</span><span style="color: #007700">() {<br />        try {<br />            throw new </span><span style="color: #0000BB">MyException</span><span style="color: #007700">();<br />        } catch (</span><span style="color: #0000BB">MyException </span><span style="color: #007700">| </span><span style="color: #0000BB">MyOtherException $e</span><span style="color: #007700">) {<br />            </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">));<br />        }<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </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></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(11) &quot;MyException&quot;
</pre></div>
    </div>
   </div>
   <div class="example" id="example-7">
    <p><strong>Exemple #7 Omettre la variable attrapée</strong></p>
    <div class="example-contents"><p>Seulement permis dans PHP 8.0.0 et ultérieur.</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">SpecificException </span><span style="color: #007700">extends </span><span style="color: #0000BB">Exception </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br />    throw new </span><span style="color: #0000BB">SpecificException</span><span style="color: #007700">(</span><span style="color: #DD0000">'Oopsie'</span><span style="color: #007700">);<br />}<br /><br />try {<br />    </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br />} catch (</span><span style="color: #0000BB">SpecificException</span><span style="color: #007700">) {<br />    print </span><span style="color: #DD0000">"Une SpecificException a été levée, mais les détails ne nous intéressent pas."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Une SpecificException a été levée, mais les détails ne nous intéressent pas.
</pre></div>
    </div>
   </div>
   <div class="example" id="example-8">
    <p><strong>Exemple #8 Throw en tant qu&#039;expression</strong></p>
    <div class="example-contents"><p>Seulement permis dans PHP 8.0.0 et ultérieur.</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br />    </span><span style="color: #0000BB">do_something_risky</span><span style="color: #007700">() or throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">"Cela n'a pas fonctionné"</span><span style="color: #007700">);<br />}<br /><br />function </span><span style="color: #0000BB">do_something_risky</span><span style="color: #007700">() {<br />    return </span><span style="color: #0000BB">false</span><span style="color: #007700">; </span><span style="color: #FF8000">// Simuler un échec<br /></span><span style="color: #007700">}<br /><br />try {<br />    </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    print </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Cela n&#039;a pas fonctionné
</pre></div>
    </div>
   </div>
   <div class="example" id="example-9">
    <p><strong>Exemple #9 Exception dans try et dans finally</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">try {<br />    try {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #0000BB">message</span><span style="color: #007700">: </span><span style="color: #DD0000">'Third'</span><span style="color: #007700">, </span><span style="color: #0000BB">previous</span><span style="color: #007700">: new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Fourth'</span><span style="color: #007700">));<br />    } finally {<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #0000BB">message</span><span style="color: #007700">: </span><span style="color: #DD0000">'First'</span><span style="color: #007700">, </span><span style="color: #0000BB">previous</span><span style="color: #007700">: new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Second'</span><span style="color: #007700">));<br />    }<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />        </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getPrevious</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),<br />    );<br />}</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(5) &quot;First&quot;
string(6) &quot;Second&quot;
string(5) &quot;Third&quot;
string(6) &quot;Fourth&quot;
</pre></div>
    </div>
   </div>
  </div>

 

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